开发者

ListView onitemclick

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> it开发者_如何学JAVAems) {
            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...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜