单击项目上的列表视图

发布于 11-14 21:30 字数 2206 浏览 3 评论 0原文

我使用 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);

,现在我想 onclick 获取标题

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

编辑:这是我的类:

private class OrderAdapter extends 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) ... 问候...

I am displaying in my listview picture and title and description using 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

I try this one but it display strange characters

EDIT: this is my class :

private class OrderAdapter extends 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心2024-11-21 21:30:30

您正在将两个适配器设置为同一个 ListView:首先是 ArrayAdapter,然后是 OrderAdapter(无论您如何定义

)一个是 OrderAdaptergetItemAtPosition (position); 检索 Order 类 的实例(或您在该适配器中使用的任何内容),而不是细绳。最有可能的是,该类不会覆盖 toString(),因此您检索一般 Object#toString() 响应:

 public String toString ()

自:API 级别 1

返回一个字符串,其中包含该对象的简洁的、人类可读的描述。鼓励子类重写此方法并提供考虑对象类型和数据的实现。默认实现相当于以下表达式:

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

如果您打算实现自己的 toString 方法,请参阅编写有用的 toString 方法。
返回此对象的可打印表示形式。

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

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

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

You are setting two adapters to the same ListView: First an ArrayAdapter<String> and then an OrderAdapter (however you define that)

Since the last one is the OrderAdapter, getItemAtPosition (position); retrieves an instance of the class Order (or whatever you use in that adapter) and not a String. Most probably, that class does not override toString(), and thus you retrieve the general Object#toString() response:

 public String toString ()

Since: API Level 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.

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. For example, create your own toString() method to return any meaningful string.

Based on your update: if I'm not mistaken, what you want to do is:

adb.setMessage("Selected Item is = "+ lv1.getItemAtPosition(position).getOrderName());
顾铮苏瑾2024-11-21 21:30:30

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

Instead of
lv1.getItemAtPosition(position).toString()
Try
a.getItemAtPosition(position).toString()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文