cnfw
cnfw

Reputation: 800

How to get String from a custom listView

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;
    }
}

Upvotes: 0

Views: 685

Answers (5)

Crimson
Crimson

Reputation: 311

but it doesn't seem to return the String, just a bunch of random stuff.

i think the bunch of random stuff you mean is the reference name to the item. If you want access to the properties of the item you should create your own class for the item and give it it's own properties e.g. for the name. I believe that is the weapon class in your case.

then you can access them in the adapter by their position and access them.

private WeaponAdapter adapter = new WeaponAdapter(getAcitivty(),R.layout.adapter_layout, Weapon)
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
        Weapon weapon = adapter.getItem(pos);
        String weaponname = weapon.getName();
    } 
});

And your weapon class should look something like this:

public class Weapon extends Activity{
    public String weaponName;
    public Weapon(){
    }
    public String getName(){
        return weaponName;
    }
}

hope it helps

Upvotes: 1

rubenlop88
rubenlop88

Reputation: 4221

You should save a reference to the adapter.

final WeaponAdapter adapter = createAdapter();
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
        Weapon weapon = adapter.getItem(pos);
        // then you can do whatever you want with the item ...
    } 
});

Upvotes: 2

Jorgesys
Jorgesys

Reputation: 126465

String sValue = (String)(listView1.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), sValue , Toast.LENGTH_SHORT).show();

or

TextView textView = (TextView) myView.findViewById(R.id.txtTitle);
Toast.makeText(getApplicationContext(), textView.getText().toString() , Toast.LENGTH_SHORT).show();

Upvotes: 1

nKn
nKn

Reputation: 13761

Try something like this:

TextView mytv = (TextView) myView.findViewById(R.id.txtTitle);
Toast.makeText(context, mytv.getText().toString(), Toast.LENGTH_SHORT).show();

Upvotes: 1

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

you can try

Item item = yourItemList[pos];

String selectedFromList = item.Name;

Upvotes: 1

Related Questions