ListView items with two rows - android

This is my ListView structure and element :
// Get a cursor with all phones
Cursor c = getContentResolver().query(Phones.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
startManagingCursor(c);
/** ----Display the Contacts on the device----- */
ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c,
new String[] { Phones.NAME, Phones.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });
setListAdapter(adapter);
and my implementation of the setOnItemClickListener is as follows:
/** ----Defining the ItemOnClickListener for the displayed List---- */
final ListView contactlistview = getListView();
contactlistview.setTextFilterEnabled(true);
contactlistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, String.valueOf(position));
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
contactlistview.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
Log.d(TAG, contactlistview.getItemAtPosition(position).toString());
}
});
and finally got the ListView as :
and when i clicked on any of the item i get the Toast Displaying the Following:
but i want my toast to display the contact name of the selected item
can u pls help me with the code :)
sorry for the inconvenience :)
THANKS :) :)

You are printing the value of the object here.
Now try like this:
Cursor c = ((Cursor) parent.getAdapter().getItem(position));
Toast.makeText(getApplicationContext(),
c.getString(Phone.NAME),Toast.LENGTH_SHORT).show();

Related

click row item but return all the rows in cursor used to build the list

I use SimpleCursorAdapter build a list. And I want to start another activity when I click one of the rows. code as following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_list);
listView = (ListView) findViewById(R.id.user_list);
db.open();
fillData();
.......
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor selectedItem = (Cursor) listView.getAdapter().getItem(position);
Log.d("#####", selectedItem.getCount() +"");
// return total number of the cursor using to build the list.
selectedItem.moveToFirst();
.......
}
});
}
private void fillData() {
Cursor c = db.fetchAllSenders();
startManagingCursor(c);
String[] from = new String[] {PeerContract.KEY_NAME};
int[] to = new int[] {R.id.user_list_row_name};
adapter = new SimpleCursorAdapter(this, R.layout.user_list_row, c, from, to, 0);
listView.setAdapter(adapter);
}
But int the LogCat Log.d("#####", selectedItem.getCount() +""); always return the total number of rows in cursor used to build the list. For example, there are 4 rows in the list, after I click one row in the list, the LogCat will display #####:4. But it supposes to be 1 when I click just one item.
Thanks in advance.
It is coming 4 because there is only one cursor reference with the adapter. the method getAdapter().getItem(position) will return you the same cursor object but it will move the cursor to the position for which you requested getItem().
So whenever you call getItem(position), it will not give you a new cursor object, it will give you the same cursor object, moving it to the current position

delete item issue from listview

i have a list view filled with data coming from database, i want when i click on an item to
disappear, i used --list.remove(position)-- but i have an error add cast to list.
HERE the list view :
private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);
String[] databaseColumnNames = new String[] { DBAdapter.col_Region };
int[] toViewIDs = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
databaseColumnNames, toViewIDs, FLAG_REGISTER_CONTENT_OBSERVER);
final ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
HERE my code when i want to delete an item :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
list.remove(position);
adapter.notifyDataSetChanged();}
There is no ListView.remove() method. You should remove correspondent item from the data model and then refresh your adapter.
In your OnItemClickListener listener get the bound data item:
Object itemObj = adapter.getItem(position);
Then, delete this item from your DB.
Then get new instance of your cursor and update your cursor adapter:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){
adapter.swapCursor(newCurosr);
} else {
adapter.changeCursor(newCursor);
}
Then, notify your list view about data changes:
adapter.notifyDataSetChanged();

Android reload ListView data from SQLite

I had a ListView that loads data from the SQLite database and onClickListener for each list that opens up to a new activity. My problem now is that when I press "Back" to go back to the previous activity with the ListView, it doesn't show.
How do I reload the list from database again? How do I go about it?
Does it have anything to do with notifyDataSetChanged()?
Please help.
Thank you.
EDITED:
Below are my codes to load the ListView under onCreate():
ListView listContent = (ListView) findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_CONTENT };
int[] to = new int[] { R.id.text };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.listrow, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent summaryIntent = new Intent(DocumentListActivity.this, ViewDocumentActivity.class);
listTopic = ((TextView) view).getText().toString();
summaryIntent.putExtra("SummTopic", listTopic);
startActivity(summaryIntent);
}
});
So what to add in the Resume()?
You can put your code to fetch data from database and adding it to ListView inside onResume(). So when you jump back to your previous Activity its onResume() will be called and your ListView will be loaded with the data from database.

To get the name of the item which was clicked

I have written the code to get the data from database and to bind it to listview now I want to click on perticular item from the listview and to get the name of that item which was clicked.
Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.row,
cursor,
columns,
to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),"cominggggg", Toast.LENGTH_SHORT).show();
}
});
}
Toast.makeText(getApplicationContext(), "Click ListItem Text "
+ ((TextView) view.findViewById(R.id.Txt))
.getText().toString(), Toast.LENGTH_LONG).show();
You can display the toast with selected item name
edit ur code like this:--
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
TextView textView = (TextView) view;
Toast.makeText(getApplicationContext(),textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});

In Android how to pass ListAdapter values to onItemClick?

I've got a functioning search interface on my app. Now I'm trying to make each search result list item clickable and display in a new activity. I've got them clickable, but I can't figure out how to pass the ListAdapter values (records) into the onItemClick method.
Here's my code so far:
private void showResults(String query) {
Cursor cursor = DBHelper.searchDB(query);
startManagingCursor(cursor);
String[] searchFrom = new String[] { DBAdapter.KEY_MAKE,
DBAdapter.KEY_YEAR, DBAdapter.KEY_MAKE,
DBAdapter.KEY_MODEL };
int[] displayHere = new int[] { R.id.rDateTV, R.id.rYearTV,
R.id.rMakeTV, R.id.rModelTV };
final SimpleCursorAdapter records = new SimpleCursorAdapter(this,
R.layout.record_2, cursor, searchFrom, displayHere);
setListAdapter(records);
DBHelper.close();
// --- Click on list item ---
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//--- here's the problem: how do I pass the ListAdapter values into onItemClick. the below doesn't like .getText()
String sYear = (view.findViewById(R.id.rYearTV)).getText().toString();
String sMake = (view.findViewById(R.id.rMakeTV)).getText().toString();
String sModel = (view.findViewById(R.id.rModelTV)).getText().toString();
// -- then pass these strings to an intent to launch a new activity
}
});
// --- END click on list item ----
}
Any ideas? I'm a rookie here, so if you could show me the code, that would be AWSOME!
In OnItemClickListener.onItemClick call parent.getItemAtPosition(position). That method returns cursor which is set to the given position. Then get your data from that cursor. Don't close cursor inside OnItemClickListener.onItemClick.
for example:
ListView clickList = getListView();
clickList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor)parent.getItemAtPosition(position)
String sMake = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_MAKE));//get data from cursor
...
}
});

Categories

Resources