I have the following layout for a listView item:
new Item(R.drawable.ic_launcher, "Item Name"),
new Item(R.drawable.ic_launcher, "Item Name 2"),
new Item(R.drawable.ic_launcher, "Item Name 3"),
And so on...
And I want to get the String (Item Name...) so, for example, I can show a toast notification showing the string name (I'll actually be using it for something else, but a toast is just for example)
Here is the code I have for the onClick of the listView, but it doesn't seem to return the String, just a bunch of random stuff.
listView1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
String selectedFromList =(listView1.getItemAtPosition(pos).toString());
Toast.makeText(getApplicationContext(), selectedFromList, Toast.LENGTH_SHORT).show();
}
});
Here is the code for my custom row layout. I've tried simply getting the String from the TextView, but that doesn't seem to work...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_vertical" />
<TextView
android:id="@+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="6"
android:gravity="center_vertical"
android:textColor="#FFFFFF"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
Here is my adapter class
public class WeaponAdapter extends ArrayAdapter<Weapon> implements SectionIndexer{
Context context;
int layoutResourceId;
Weapon data[] = null;
public WeaponAdapter(Context context, int layoutResourceId, Weapon[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
WeaponHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new WeaponHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (WeaponHolder)row.getTag();
}
Weapon weapon= data[position];
holder.txtTitle.setText(weapon.title);
holder.imgIcon.setImageResource(weapon.icon);
return row;
}
static class WeaponHolder
{
ImageView imgIcon;
TextView txtTitle;
}
@Override
public int getPositionForSection(int sectionIndex) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getSectionForPosition(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object[] getSections() {
// TODO Auto-generated method stub
return null;
}
}
Viewfor that layout?listView.setAdapter?