is possible get the value of a single row.
In partycular i wont get in a string the value of id in my row of mt list with adapter.
In each row there are value id and label
Thaks a lot and sorry for my english
ListView list;
list = (ListView) findViewById(R.id.listView1);
mSchedule = new SimpleAdapter(getApplicationContext(), result ,R.layout.row_meteo,
new String[]{"id", "label"}, new int[] {R.id.textView1,R.id.textView2});
list.setAdapter(mSchedule);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String idPlace = mSchedule.getItem(position).toString();
Toast.makeText(getApplicationContext()," Cliccato sul luogo :"+idPlace,Toast.LENGTH_LONG).show();
}
}
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.textView1); //<---------
String str = tv.getText();
}
Replace
String idPlace = mSchedule.getItem(position).toString();
with
String idPlace =((TextView)view.findViewById(R.id.textView1)).getText().toString();
Related
How can I get a item from a listView 2 items , I am using to fill the listView a database of queries but there do not know how to do that to get the name of the item ?
from = new String[]{manager.CN_NAME,manager.CN_CREDITS};
to = new int[]{android.R.id.text1,android.R.id.text2};
manager = new DataBaseManager(this);
listaMateria = (ListView) findViewById(R.id.listView);
cursor = manager.cargarCursorMateria();
adapter = new SimpleCursorAdapter(this,android.R.layout.two_line_list_item,cursor,from,to,0);
listaMateria.setAdapter(adapter);
listaMateria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String item = ((TextView)view).getText().toString();
String a = Integer.toString(to[position]);
Toast.makeText(getBaseContext(), a, Toast.LENGTH_LONG).show();
}
});
You could also get the cursor for that row.
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Cursor cursor = (Cursor)adapter.getItem(position);
String value = cursor.getString(cursor.getColumnIndex("column_name"))
}
I have an OnItemClick(), after that I wanted to get the ItemClicked name's and display it in a TextView.I do it normally, but the problem is that I get the ItemClicked name's inside a bracket! How do I avoid this?Here is the output:
{titre=hello}
I want only in my TextView:
hello.
Here is the code:
public void onItemClick(AdapterView<?> parent, View view, int position, long id ) {
displayTextViewTitle = (TextView)findViewById(R.id.text_view_title);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
Try this :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
TextView item = (TextView) parent.getItemAtPosition(position);
String value = item.getText();
displayTextViewTitle.setText(value);
}
or even better try :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
ListView lv = (ListView) findViewById(yourListViewName);
String value = lv.get(position).tostring();
displayTextViewTitle.setText(value);
}
If you are getting your String {titre=hello} like this after click on ListView. then you should use this:
String[] no = item.split("=");
String m = no[0];
String k = no[1];
String name = k.substring(0, k.length() - 1);
name is your final result String .
value is already a String, you don't have to use String.valueOf(value).
The listener method works fine, check this:
public class SplashActivity extends Activity implements OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ListView lv = (ListView)findViewById(R.id.lv);
ArrayList<String> members = new ArrayList<String>();
members.add("titre=hello");
members.add("world");
ArrayAdapter s = new ArrayAdapter(this, android.R.layout.simple_list_item_1, members);
lv.setAdapter(s);
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
}
Recheck the values inside your listview, the problem might be there
I'm beginning learn Android. I have issue. I have autocompleteTextView with content is arrayList. Then I want to get position of element in arraylist when I click on autoCompleteTextView. Can you show me do it ?
Thank you so much !
This code !
/* ================= looping through All categories ==================== */
for(int i=0;i<product.length();i++){
JSONObject c = product.getJSONObject(i);
// Storing each json item in variable
pro_id = c.getString(TAG_ID);
pro_name = c.getString(TAG_NAME);
name_product.add(pro_name);
} // end for products
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_product);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.result_search);
textView.setThreshold(1);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//here position is your selected item id
/*String selecteditem =name_product.get(position);
Log.d("Test",selecteditem.toString());
int pos = Integer.parseInt(selecteditem);
Toast.makeText(getApplicationContext(), "cvxcvcv",pos).show();*/
// I want to get position when i click element in autocompletetextView
}
});
Try
autocompletetextview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//here position is your selected item id
int selectedposition=position;
}
});
My current problem is I have a ListView I'm using to display my contacts now I'd like to set the onItemClick to grab the name of the contact and set that to a TextView called p2 the code I currently have is:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.setp2);
cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
String[] from = new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to = new int[]{R.id.id_text,R.id.name_text};
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(setp2.this,R.layout.setp2_layout,cursor,from,to);
ListView list = (ListView)findViewById(R.id.contact_list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
}
So basically I'm unsure of what I need to do in the onItemClick, any help appreciated!
In your onItemClick, you will get the instance of the view associated with that list item.From that view you can get the text associated with it and set it to the p2 TextView. If my understanding of your requirement is correct, the code will be something like this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CharSequence name = ((TextView)view.findViewById(R.id.name_text)).getText();
//Set this to your textview in P2
tvP2.setText(name);
}
});
Set up a SimpleCursorAdapter.CursorToStringConverter and set it on the adapter, then use convertToString(Cursor cursor) to get the text in on item click :
(Cursor) simple.getItem(position)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
cursor.moveToPosition(position);
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
p2.setText(name);
}
});
I have an Activity with ListView took in my Database. I would like to, when I select one, get the id field.
lv = (ListView) findViewById(R.id.listMessageConversationView);
Cursor c = selectInfoInDB();
int[] to = new int[] {R.id.idMessageClavier, R.id.nomMessageClavier, R.id.valeurMessageClavier, R.id.groupeMessageClavier, R.id.occurrenceMessageClavier};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.conversation_clavier_display_data, c, SmartAccess_v1Activity.nomColonnesMessage, to);
lv.setAdapter(sCA);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)findViewById(R.id.idMessageClavier);
}
});
The fields in the ListViews are correct, but the toast give me the id of the first item of ListView, wherever I touch on list.
I worked on that and i I can't figure where is the mistake -_-
Thanks for your help, korax
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int postion,
long id) {
Toast.makeText(this, "id is :: " + id +"position :: " + position,
Toast.LENGTH_SHORT).show();
}
});