gpt4 book ai didi

java - ListView onitemclick

转载 作者:行者123 更新时间:2023-11-29 03:56:09 26 4
gpt4 key购买 nike

我正在使用 arrayadapter 在我的 ListView 中显示图片和标题以及描述

lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));*/
m_orders = new ArrayList<Order>();

this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
final ListView lv1=(ListView)findViewById(R.id.list);
lv1.setAdapter(this.m_adapter);
lv1.setTextFilterEnabled(true);

现在我想通过点击获取标题

我试试这个,但它显示奇怪的字符

编辑:这是我的课:

私有(private)类 OrderAdapter 扩展 ArrayAdapter {

    private ArrayList<Order> items;

public OrderAdapter(Context context, int textViewResourceId, ArrayList<Order> items) {
super(context, textViewResourceId, items);
this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Order o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
if (tt != null) {
tt.setText("Name: "+o.getOrderName()); }
if(bt != null){
bt.setText("Status: "+ o.getOrderStatus());
}
}
return v;
}
}


lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListViewExampleActivity.this);
adb.setTitle("The Selected Item");
//lv1.getItemAtPosition(position)
adb.setMessage("Selected Item is = "+ lv1.getItemAtPosition(position).toString());
adb.setPositiveButton("Ok", null);
adb.show();
}
});

所以我如何指定要显示的项目,如 arraylist(0,1) ...问候...

最佳答案

您正在将两个适配器设置为相同的 ListView : 首先是 ArrayAdapter<String>然后是 OrderAdapter (无论你如何定义)

因为最后一个是 OrderAdapter , getItemAtPosition (position);检索 class Order 的实例(或您在该适配器中使用的任何内容)而不是字符串。最有可能的是,该类不会覆盖 toString() ,因此您检索了一般 Object#toString()回应:

 public String toString ()

自:API 级别 1

Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:

getClass().getName() + '@' + Integer.toHexString(hashCode())

See Writing a useful toString method if you intend implementing your own toString method. Returns a printable representation of this object.

编辑:澄清一点我的回答:问题不在监听器内部。看起来不错。您将需要研究如何设置适配器以及如何(如果)定义任何类。例如,创建您自己的 toString() 方法以返回任何有意义的字符串。

根据您的更新:如果我没记错的话,您想要做的是:

adb.setMessage("Selected Item is = "+ lv1.getItemAtPosition(position).getOrderName());

关于java - ListView onitemclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6325442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com