Applying SQLite Array to Scrolling Activity? - java

Firstly, I'm extremely unsure on the majority of Android coding, this is only my second day doing it, so please excuse any mistakes I make in my explanations!
I've got a database via SQLite and currently display my data via a listView. Currently, it works fine displaying the data and I can scroll through and click on it no issues. However, I'd like to place my data within the "Scrolling Activity" template you can find in Android Studio. From Googling around, I understand somewhat that you can't place a listView in a nestedScrollView, which is what the activity uses.
I am unsure, however, how to display my data from the database without using a listView. Could somebody please help me either convert the listView into something compatible, or explain a way to combine them (Hacky methods are fine for now!)
I've displayed all of the necessary code below.
MainActivity.java:
mydb = new DBHelper(this);
ArrayList array_list = mydb.getAllCotacts();
ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
obj = (ListView)findViewById(R.id.listView1);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
obj.setEmptyView(emptyText);
obj.setAdapter(arrayAdapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int id_To_Search = arg2 + 1;
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", id_To_Search);
Intent intent = new Intent(getApplicationContext(),DisplayContact.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
content_scrolling.xml:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<android.support.v7.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
</android.support.v7.widget.LinearLayoutCompat>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/emptyText"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</android.support.v4.widget.NestedScrollView>
Apologies for any mistakes, as I say, this is my second day; I'm learning as quick as I can!

You can create views programmatically and add them to LinearLayout on the fly. You can use any kind of view, but in this example I wanted to keep things clear so I assumed that your contacts are Strings and you want to just show them as TextViews. You can try something like this:
mydb = new DBHelper(this);
List<String> contacts = mydb.getAllCotacts();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
for(String data : contacts) {
//Create a view for an LinearLayout
TextView textView = new TextView(this);
//Do whatever you like with a view - add listener, adjust style, add text etc.
textView.setOnClickListener(...);
textView.setText(data);
textView.setGravity(Gravity.CENTER);
//Add textView to linearLayout
linearLayout.addView(textView);
}
xml (not very pretty, customize it if you like :))
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="uk.ac.tees.q5065885.diary.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/linear_layout"
android:orientation="vertical"/>
</android.support.v4.widget.NestedScrollView>

Related

How can i center textviews in a table row that is constantly being generated dynamically?

I have a zone list. And my function creates a table row for each zone and adds a textview of zone's name to this row and then adds this row to my TableLayout. Since the zonelist is populated from a database, the list may change during the lifetime of app.
What I'm trying to do is to center each textview in each row horizontally. I'm checking every source and people keep saying "add this piece of code to your XML file...". Since I'm creating these rows and textviews dynamically, I don't have any objects to manipulate in my XML file. So I have to somehow manage it by coding in Java side. Any help would be appriciated.
private void addTextView(final Zone zone) {
TableRow row = new TableRow(DashboardFragment.this.getActivity());
TextView textView = new TextView(DashboardFragment.this.getActivity());
textView.setId(zone.getId());
textView.setText(zone.getName());
textView.setTextColor(Color.BLACK);
textView.setTextSize(25);
TextView seperator = new TextView(DashboardFragment.this.getActivity());
seperator.setText("_____________________________________________________________________________________________________________________________");
seperator.setTextSize(25);
seperator.setTextColor(Color.BLACK);
TableRow seperate = new TableRow(DashboardFragment.this.getActivity());
seperate.addView(seperator);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity().getApplication(), ZoneContentsActivity.class);
intent.putExtra("zone",zone);
startActivity(intent);
}
});
row.addView(textView);
layout.addView(row);
layout.addView(seperate);
}
(Note: I know the way I seperate is not the best xD. So any help on this side would be appriciated as well.)
Try this:
In layout folder, (layout_row.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/nameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center" />
<include layout="#layout/line_horizontal_gray" />
</LinearLayout>
</TableRow>
And for adding Row to table layout:
private void addRow(final Zone zone){
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow rowView = (TableRow)inflater.inflate(R.layout.layout_row, null);
TextView textView = rowView.findViewById(R.id.nameTxt);
textView.setText(zone.getName());
textView.setTextColor(Color.BLACK);
textView.setTextSize(25);
layout.addView(rowView);
}
Seperator is added on the layout_row. Hope it might help.
line_horizontal_gray.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray">
</LinearLayout>
You can try
textView.setGravity(Gravity.CENTER_HORIZONTAL);

How can I add a text view for every item in my list?

I am making a yelp-like app in which users can leave reviews for various places, but I want other users to be able to see all of the reviews left. Currently, I am using a list to store all of my reviews in, but I can't seem to figure out how I can make a textview for each item in the list, seeing as the length of the list may vary. Also, piggy backing on this question, would there be anyway to format the textview (i.e. layoutbelow or margins)?
Thanks in advance,
Jacob
Should be able to do this with an adapter and listview:
Activity.java:
public class YourActivity extends Activity
{
ArrayList<String> reviewsArray = new ArrayList<String>();
ListView reviewList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.youractivity);
reviewList = (ListView) findViewById(R.id.yourListViewId);
//fill your reviewsArray...
ArrayAdapter<String> reviewsAdapter = new ArrayAdapter<String>(context, R.layout.reviewLayout, reviewsArray);
reviewsList.setAdapter(reviewsAdapter);
}
}
activity_layout.xml: (change size of the listview here)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/yourListViewId"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
reviewLayout.xml (change the reviews padding etc here)
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/gridCell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
Without seeing your code it's hard to give you an exact example, but this contains all the basics.

Custom ListView TextView Items all show gray bar at top

I'm having a problem with a custom ListView I am using in the ListActivity of my application. My problem is that all TextView items added to the ListView through an ArrayAdapter show up with a gray bar above them. I would include an image of the displayed ListView, but am unable since I do not have a reputation of 10 required by the stackoverflow site.
The layout file (index.xml) used to produce the ListView is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
<ListView
android:divider="#color/mg_red"
android:dividerHeight="1sp"
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:title="text_view"
android:background="#drawable/listitemback"
android:cacheColorHint="#drawable/listitemback"
android:id="#+id/listItem"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10sp"
android:textColor="#color/listitemtext"
android:textSize="16sp" />
</LinearLayout>
The ListActivity code used to display the list is as follows:
public class IndexListActivity extends ListActivity
{
private ListView m_listView = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
try
{
if (MGApplication.DEBUG_BUILD)
Log.i("TMG", "IndexListActivity.onCreate");
super.onCreate(savedInstanceState);
// Get our global data object.
MGApplication mgApp = (MGApplication) getApplication();
// Set view layout
SetContentView(R.layout.index);
// Get references to our ListView and AdView objects
m_listView = (ListView) findViewById(android.R.id.list);
// Create a new ArrayAdapter object that will be used to initialize
// the underlying ListView object.
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.index,
R.id.listItem,
mgApp.m_strAZRhymeNames);
// Assign array adapter
m_listView.setAdapter(aa);
}
catch (Exception e)
{
}
return;
}
}
Any help is greatly appreciated as I am at my wits end with this issue. I think I've tried every suggestion I could find on the web and I am unable to remove the gray bar.
Thank You,
Bob
you have to set this property in test.xml file with.
<ListView
android:id="#+id/listview_middle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dip"
android:layout_weight="1"
android:background="#ffffff"
android:cacheColorHint="#android:color/transparent"
android:choiceMode="singleChoice"
android:scrollingCache="false" />
and set some property in listview object.
lv.setVerticalFadingEdgeEnabled(false);
In my side that's work perfect.

How to create an ListView without ListActivity

Someone could tell me how can I create a ListView without using a ListActivity?
I need to put in my layout several other views, and I wish the layout was not fully occupied by the ListView. It must be part of the layout, not the entire layout. I want to create a normal Activity.
Code snippet:
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.savings);
//...
ListView lvPoupanca = (ListView)this.findViewById(R.id.lvPoupanca);
// the adapter
TestRepository repo = new TestRepository(this);
Cursor c = repo.getCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c, new String[]{"Nome", "ValorAtual"}, new int[] {R.id.tvNome, R.id.tvValorTotal});
lvPoupancas.setAdapter(adapter);
// ...
}
Edit:
The saving.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvPoupancas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
Hope somebody can help me.
Thanks and sorry about my bad english.
Like with any other View in any Activity...
ListView l = new ListView(this);
// OR
ListView l = (ListView) findViewById(R.id...);
...
l.setAdapter(...);
// If using first way: setContentView(l);
For example
I have replied to a similar question before. But that was in a different context. So, I'm putting some reference code here. It will help you to fix the issue.
public class ListandtextActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String [] str = {"ONE","TWO","THREE"};
final ListView lv = (ListView) findViewById(R.id.listView1);
final TextView tv = (TextView)findViewById(R.id.tv1);
ArrayAdapter<Object> adapt = new ArrayAdapter<Object>(getApplicationContext(), android.R.layout.simple_list_item_1, str);
lv.setAdapter(adapt);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tv.setText("You Clicked Something");
}
});
}
}
And the XML is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" android:textSize="20dip"/>
</LinearLayout>
By the way, before asking questions, collect as much information as possible for others to help you. If it does not work, it will be better to put the error log in the question rather than just saying it does not work.
Don't Extend ListActivity then. Extend an Activity and in onCreate() put the following code as per your requirement.
setContentView(R.layout.ur_xml);
Listview list = (ListView)findViewById(R.id.lvPoupancas);
list.setAdapter(your Adapter);

Change properties of TextView in ListView row xml

Hey, I want to change the TypeFace of one of the TextViews to an external font.. How can I do that?
Code:
public class English extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
This crashes because of NullPointer:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
So how can I reach the TextView that is in the row to change its TypeFace? Thanks
You can try to get the layout for each list item by
View rowView = adapter.getView(...);
Then get the TextView of the layout by
TextView txt = (TextView)rowView .findViewById(R.id.QuranEnglishTextViewEnglish);
txt.setTypeFace(...);
and set your TypeFaces. Haven't tested that yet, but i guess it could work.
But I recommend extending your own BaseAdapter. Then create another list-row class which provides the inflated layouts for your BaseAdapter. There you'll be able to set the typefaces easily for every TextView.
try this links it works http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html see steps and class MyTextView and MyButton shown above layout

Categories

Resources