如何从自定义listView获取String

时间:2014-02-04 19:18:39

标签: android listview android-listview

我有一个listView项目的以下布局:

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"),

等等......

我想得到字符串(项目名称......)所以,例如,我可以显示一个显示字符串名称的Toast通知(我实际上会将其用于其他内容,但是Toast仅适用于例如)

这是我对listView的onClick所拥有的代码,但它似乎没有返回String,只是一堆随机的东西。

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

以下是我的自定义行布局的代码。我试过从TextView中获取String,但这似乎不起作用......

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

这是我的适配器类

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

5 个答案:

答案 0 :(得分:2)

您应该保存对适配器的引用。

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

答案 1 :(得分:1)

你可以尝试

Item item = yourItemList[pos];

String selectedFromList = item.Name;

答案 2 :(得分:1)

尝试这样的事情:

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:1)

  

但它似乎没有返回String,只是一堆随机的东西。

我认为你所说的一堆随机内容是该项目的引用名称。 如果您想要访问该项目的属性,您应该为该项目创建自己的类,并为其提供自己的属性,例如:为了这个名字。我认为这是你的武器类。

然后您可以通过位置访问适配器并访问它们。

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

你的武器类应该是这样的:

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

希望有所帮助