How to inflate the XML layout using Custom ArrayAdapter Class? - java

In my app,I need to have delete button in my listview,So to do that I refer this link ListView with Add and Delete Buttons in each Row in android.
I refer the first answer,but the problem now is,in that they are adding items to the listview like this.
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
But ,I fetch data to listview using cursor.Below one is my code to fetch data from database to listview.
private void fetchData() {
db = helper.getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row,
c,
new String[]{DBhelper.Name},
new int[]{R.id.lblreload});
rldlist.setAdapter(adapter);
}
Now I don't have any idea,how to modify that code.please help me to solve this.

You can subclass CursorAdapter and provide implementations for newView() and bindView().

Related

ArrayAdapter with Json Parsing

i'm doing a system of news feed, so, i've a difficulty for adapter my ArrayAdapter insert value into ListView, with: "username, name, picture, content and others values" i'm newbie in Android Studio.
Searching the internet I found this function
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
But, i use ArrayList and ArrayAdapter:
ArrayList<Post> postList =
new JsonConverter<Post>().toArrayList(s, Post.class);
ArrayList<String> postador = new ArrayList<String>();
for(Post value: postList){
postador.add(value.id_postador);
postador.add(value.post_legenda);
if(value.post_foto != null) {
postador.add(value.post_foto);
}
postador.add(value.post_data);
postador.add(value.curtidas);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_list_item_1, postador
);
lvPost.setAdapter(adapter);
I tried it, but no success.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item, postador, new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
There a method convert ListAdapter in ArrayAdapter or adapter my ArrayAdapter for ListAdapter?
If you are a newbie to Android, I would like to recommend few things to you.
1). Start Using RecyclerView from the very beginning instead of ListView. It is more advanced, flexible and efficient version of ListView. You can find a sample demo for the RecyclerView here and here
2). If you still want to use ListView. Then you further have two options, either to use BaseAdapter or ArrayAdapter.
a). Base Adapter as the name suggests, is a base class for all the adapters. If you need more flexibility, you can go with it. You can find the demo sample here and here
b). On the other hand, ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.To use something other than TextViews (like in your case) for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. You can find the demo sample here and here.
Hope it will help you in future and also I suppose you will get answer to your question in the specified demo samples. Thanks and have a good day.. :)

How to use findViewById to get a ListView

I don't know why this code is using findViewById to get the list view. Isn't the list view native to the ContentView that I set earlier? If not how do I set up a ListView for that Content View? I really just want to know if the setting up of the list view is necessary.
setContentView(R.layout.imgp_activity_home);
final String[] values = new String[] {"No Docs", "Pending", "Processed"};
final ListView listview = (ListView) findViewById(R.id.???);
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i<values.length; i++) {
list.add(values[i]);
...
listview.setAdapter((ListAdapter) adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
...
I feel like I should be able to add the listener to the array list somehow.
This is the tutorial I used to get me started.
in you layout floder about the activity layout the XML file called imgp_activity_home.xml.in this file you may have the tags about<listview>. set tags like <listview android:id="#+id/listname">.then in the Java file you can findviewbyid(r.id.listname).if you didn't declare the listview tag.you must new a listview object in you code.
From the example you should write
final ListView listview = (ListView) findViewById(R.id.listview);

Android - Displaying all SQLite database entries in a ListView

What I am trying to do is display the contents of the database in a ListView. The layout contains a ListView which I have used and implemented but I can't seem to get them working together (or even the cursor), could someone give me a hand and an explanation of why my cursor implementation doesn't work?
I have a method to return the database entries as a Cursor:
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TEXT}, null, null, null, null, null);
}
I have the class where I want to insert and display the database entries:
Button add;
EditText tM;
ListView generalList;
DBAdapter dba = new DBAdapter(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_general);
add = (Button) findViewById(R.id.button1);
tM = (EditText) findViewById(R.id.editText1);
generalList = (ListView) findViewById(R.id.generalList);
Cursor c = dba.getAllRecords();
c.moveToFirst();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.id.generalList, c, new String[] {dba.KEY_TEXT}, new int[] {R.id.generalList}, 0);
// This doesn't seem to work for me, I don't know how to fix it
// or how to then get it working with the ListView
generalList.setAdapter(adapter);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
insertIntoDatabase();
}
});
}
public void insertIntoDatabase() {
dba.open();
dba.insertRecord(textMessage.getText().toString());
Toast.makeText(this, "Added:\n" + tM.getText().toString(), Toast.LENGTH_LONG).show();
dba.close();
}
When using a ListView with a CursorAdapter, the Cursor returned from the column must contain a column with the name _id uniquely identifying each row. I'm guessing that the one column you are fetching (dba.KEY_TEXT) is not named "_id". You can either add a column to your table named _id or when you perform your SELECT have the database return the name as _id
i.e.
SELECT col_name as _id FROM my_table;
OR
SELECT col_name _id FROM my_table;
if the list contains only 1 TextView, you can use android.R.layout.simple_list_item_1 instead of your R.id.generalList.
and change the new int[] {R.id.generalList} to new int[] {android.R.id.text1}
like :
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[] {dba.KEY_TEXT}, new int[] {android.R.id.text1}, 0);
on the arguments that receives the simplecursoradapter, remember the layout it receives is the layout for the actual item not the listview. so remove the R.id.generalList from there, cause that is the id that identified the listview not a full layout. so replace that with a layout that contains a textview. now, on the int array goes the id of the textview that will show the text you want and on the string array pass on the names of the fields in the record read from the database. do as mentioned by aprian and know that you can customize items as much as you need.

Getting the row ID of a listview from a Android SQlite database

I have created a listview which displays all the people in the database. But how do I get the row id of the one I select so I can pass that infomation to the next activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_game1);
listContent = (ListView)findViewById(R.id.contentlist);
mDbAdapter = new DbAdapter(this);
mDbAdapter.open();
Cursor cursor = mDbAdapter.fetchAll();
startManagingCursor(cursor);
String[] from = new String[]{DbAdapter.KEY_NAME, DbAdapter.KEY_ROWID};
int[] to = new int[]{R.id.text};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mDbAdapter.close();
}
Which displays
displays http://i.minus.com/iGEtGKGyF1Wgc.png
Now I want to click on a name get that row id from the database.
Use this API call: cursorAdapter.getItemId(positionInListView)
Use setOnItemClickListener on your Listview then you will get the id of the selected item (item id start from 0 but Database _id column is start from 1 so you should send id+1, please confirm this).
And send the id using intents.
You have to register implement and add an OnItemClickListener to the list so you can react when an item was clicked. The item id will be passed into the onListItemClick() method of the OnItemClickListener.

ListView in xml, setting Adapter to

I have following code:
public class ShowActivity extends ListActivity implements OnItemClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.show_list);
ListView lv = getListView();
String[] projection = new String[]{
BaseColumns._ID,
DepotTableMetaData.ITEM_NAME,
DepotTableMetaData.ITEM_AMOUNT,
DepotTableMetaData.ITEM_UNIT,
DepotTableMetaData.ITEM_PPU,
DepotTableMetaData.ITEM_TOTAL,
DepotTableMetaData.ITEM_COMMENT};
Cursor c = managedQuery(ContentProviderMetaData.DepotTableMetaData.CONTENT_URI, projection, null, null, ContentProviderMetaData.DepotTableMetaData.DEFAULT_SORT_ORDER);
String[] columns = new String[]{BaseColumns._ID,ContentProviderMetaData.DepotTableMetaData.ITEM_NAME, ContentProviderMetaData.DepotTableMetaData.ITEM_AMOUNT, ContentProviderMetaData.DepotTableMetaData.ITEM_UNIT, ContentProviderMetaData.DepotTableMetaData.ITEM_PPU, ContentProviderMetaData.DepotTableMetaData.ITEM_TOTAL, ContentProviderMetaData.DepotTableMetaData.ITEM_COMMENT};
int[] to = new int[]{R.id.lname,R.id.lamount,R.id.lunit,R.id.lppu,R.id.ltotal,R.id.lcomment};
Log.d("ShowActivity","Cursor, columns, to set - now setting adapter");
SimpleCursorAdapter simpleadapter = new SimpleCursorAdapter(this,R.layout.list_entry, c, columns, to);
// ListView lv = (ListView)findViewById(R.id.ListView);
this.setListAdapter(simpleadapter);
lv.setOnItemClickListener(this);
// intentCheck();
}
It works fine, but as you might have seen at my comments, i don't want to create the listview via getListView(), i want to do a usual setContentView(R.Layout.---) and populate a Listview, which is defined there. But all tutorials I have seen yet do it like that, and I have no clue how to change that to my wishes.
How does my layout-xml have to look like? how do i "tell him to use it" ?
Thanks in advance.
You can use directly setContentView(R.layout.main); on a ListActivity as long as you define a listView with the android:list id on your xml:
<ListView android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
To set the adapter call directly setListAdapter from your ListActivity
Don't use ListActivity as your base class, extend Activity, set your content to the layout, grab a reference to your expanded ListView (findViewById(...)) and then handle setting the adapter that way.
ListView lv = (ListView)findViewById(android.R.id.list);
No change needed in the .xml

Categories