简体   繁体   English

ListView onitemclick

[英]ListView onitemclick

I am displaying in my listview picture and title and description using arrayadapter我正在使用 arrayadapter 在我的列表视图图片和标题和描述中显示

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

and now I want onclick to get the title现在我想要 onclick 获得标题

I try this one but it display strange characters我试试这个,但它显示奇怪的字符

EDIT: this is my class:编辑:这是我的 class:

private class OrderAdapter extends ArrayAdapter {私有 class 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();
      }
      }); 

so how I can specify which Item I want to display like arraylist(0,1)... regards...那么我如何指定要显示的项目,如arraylist(0,1)......关于......

You are setting two adapters to the same ListView : First an ArrayAdapter<String> and then an OrderAdapter (however you define that)您将两个适配器设置为同一个ListView :首先是一个ArrayAdapter<String>然后是一个OrderAdapter (但是你定义它)

Since the last one is the OrderAdapter , getItemAtPosition (position);由于最后一个是OrderAdapter ,所以getItemAtPosition (position); retrieves an instance of the class Order (or whatever you use in that adapter) and not a String.检索class Order (或您在该适配器中使用的任何内容)的实例,而不是字符串。 Most probably, that class does not override toString() , and thus you retrieve the general Object#toString() response:最有可能的是, class 不会覆盖toString() ,因此您检索一般的Object#toString()响应:

 public String toString ()

Since: API Level 1自:API 1 级

Returns a string containing a concise, human-readable description of this object.返回包含此 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.如果您打算实现自己的 toString 方法,请参阅编写有用的 toString 方法。 Returns a printable representation of this object.返回此 object 的可打印表示。

EDIT : To clarify a little bit my answer: The problem is not inside the Listener.编辑:澄清一下我的回答:问题不在监听器内部。 That looks fine.看起来不错。 You will need to work on how you set up the adapter and how (if) you define any class.您将需要研究如何设置适配器以及如何(如果)定义任何 class。 For example, create your own toString() method to return any meaningful string.例如,创建您自己的 toString() 方法以返回任何有意义的字符串。

Based on your update : if I'm not mistaken, what you want to do is:根据您的更新:如果我没记错的话,您想要做的是:

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

Instead of lv1.getItemAtPosition(position).toString() Try a.getItemAtPosition(position).toString()而不是 lv1.getItemAtPosition(position).toString() 尝试 a.getItemAtPosition(position).toString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM