How to get object value from listview adapter position - android

How to get value from adapter position, i have code in below:
CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;
try {
categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };
final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
categories, R.layout.per_item_category, from, to);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
If I look log it on logcat in the MyLog I get as:
Value is: {position=12,
image_path=http://192.168.103.121/xml/icon.png,
link=http://192.168.103.121/xml/category.php?kat_id=13, name=Category
13}
So my question, I want to get value from name and stored to variable String name, I want to get just "Category 13" in String name. Because I want to passing it to another activity.

Looks like you made ​​the object with hashmap, such blablabla.put("name", "value")?? If yes. Try this:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});
Change to:
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String name = (String) obj.get("name");
Log.d("Yourtag", name);
}
});

value = value.substring(value.indexOf("name=")+5,value.length()-1);

listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// TODO Auto-generated method stub
Object obj=listID.getAdapter().getItem(position);
String str=obj.toString();
Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
}
});

You can also get the adapter data via AdapterView<?> parent
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//setHasOptionsMenu(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Filedata mnotes = (Filedata) parent
.getItemAtPosition(position);
Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}

This is what i did:-
lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
Log.d("val:",selectedFromList.getDate());
Log.d("val:",selectedFromList.getMobile_no());
}
});

listID.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
HashMap<String, Object> sdsa = (HashMap<String, Object>) adapter.getItem(position);
Log.e("IndentNo", (String) sdsa.get("IndentNo"));
Log.e("IndentId", (String) sdsa.get("IndentId"));
}
});

Related

how can i change my search with search position by id?

I have two spinner the second one is to link with the first one with position and a list view that shows a CV when you click on an item that is linked with position too, I want to replace it will be linked with id, how can I do please
this is my code spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
if(arrayListTache.size() > position)
{
spinner_tache.setSelection(position);
pos = position;
valueInteger = arrayListInteger.get(position);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
my code onclick list view
SubjectListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int
pos, long id) {
String selectedItem =
parent.getItemAtPosition(pos).toString();
Intent intent = new
Intent(getApplicationContext(),Main2Activity.class);
intent.putExtra("Position" , String.valueOf(id));
startActivity(intent);
// insert();
}
});

Mapping items in list view to number of clicks

I have a ListView populated with a custom Array adapter. I want to get the total number of clicks on each item in the ListView .How can i map the listview item id and number of clicks on that item for multiple items?
Use this code.. exact.. hardcoded the whole code for you :p
HashMap<Integer,Integer> map_positions_and_count = new HashMap<>();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i<listView.getAdapter().getCount()-1; i++)
{
if (i == position)
{
if (map_positions_and_count .get(position) !=null)
{
map_positions_and_count .put(position,map_positions_and_count .get(position)+1);
}
else {
map_positions_and_count .put(position,1);
}
}
}
// map.put(position,)
}
});
You can use this in your mainActivity ..
You can try this in your activity,
ArrayList<Integer> arrayList=new ArrayList<>();//make this as global
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position)!=null) {
int a = arrayList.get(position);
arrayList.set(position, a+1);
}
else
arrayList.set(position,1);
}
});
Try this code ->
final HashMap<Integer, Integer> listItemCount = new HashMap<Integer, Integer>();
//Set an onItemClickListener
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (listItemCount.containsKey(position)){
listItemCount.put(position, listItemCount.get(position)+1);
}else {
listItemCount.put(position, 1);
}
}
});

How do I get the generated key for an item add to Firebase

Trying to get the generated key for item added, when using the onItemClickListener,
Please see my code, :
ref = new Firebase("https://.......firebaseio.com/Service");
final FirebaseListAdapter<MainCategory> adapter = new FirebaseListAdapter<MainCategory>(getActivity(), MainCategory.class,
android.R.layout.simple_list_item_1,
ref) {
#Override
protected void populateView(View view, MainCategory s, int i) {
TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText(s.getTitle());
}
};
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemId = String.valueOf(parent.getId());
String root = String.valueOf(ref.getRoot().getKey());
MainCategory object = (MainCategory) parent.getItemAtPosition(position);
Toast.makeText(getContext(), itemId+"____"+"__"+root, Toast.LENGTH_SHORT).show();
}
});
You're looking for:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String key = adapter.getRef(position).getKey()
}

How to implement onClick in a Listview

I guess this is simple.
I want to toast the text in the rows when I click on it. And here is my code:
public class AndroidSQLite extends Activity { private SQLiteAdapter
mySQLiteAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listContent = (ListView)findViewById(R.id.contentlist);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_NOME};
int[] to = new int[]{R.id.text};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
mySQLiteAdapter.close();
}
}
Thank You!
just write down
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1,
int position, long id) {
Log.i("get ItemIDPosition",
"" + adapter.getItemIdAtPosition(position));
Log.i("get ItemATPosition", "" + adapter.getItemAtPosition(position));
}
});
You can use
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
Like this:
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ...;
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
Try this code for Item click
listContent.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String text = <Get Your from database is here in this string>;
Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
}
});
Use onitemclick evetn and not onclick.
listview.setOnItemClickListener(...);
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
arg2 is the index in the list.

How to get the string value of selected value item in the ListView

I have a Listview in my project for which i want to find out the string value of the selected item in the listview.
Here is a simple Examlple.
#Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
From your Activity
public class ExampleActivity extends Activity {
String str_arr[] = {"A", "B", "C"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, str_arr));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
// TODO Auto-generated method stub
Object o = adapter.getItemAtPosition(position);
String str_text = o.toString();
Intent intent = new Intent(ExampleActivity.this, NewExample.class);
intent.putExtra("StrValue", str_text);
startActivity(intent);
}
});
}
}

Categories

Resources