单击项目上的列表视图
我使用 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...
您正在将两个适配器设置为同一个
ListView
:首先是ArrayAdapter
,然后是OrderAdapter
(无论您如何定义)一个是
OrderAdapter
,getItemAtPosition (position);
检索Order 类
的实例(或您在该适配器中使用的任何内容),而不是细绳。最有可能的是,该类不会覆盖toString()
,因此您检索一般Object#toString()
响应:自:API 级别 1
编辑:为了澄清一点我的答案:问题不在监听器内部。看起来不错。您将需要研究如何设置适配器以及如何(如果)定义任何类。例如,创建您自己的 toString() 方法以返回任何有意义的字符串。
根据您的更新:如果我没记错的话,您想要做的是:
You are setting two adapters to the same
ListView
: First anArrayAdapter<String>
and then anOrderAdapter
(however you define that)Since the last one is the
OrderAdapter
,getItemAtPosition (position);
retrieves an instance of theclass Order
(or whatever you use in that adapter) and not a String. Most probably, that class does not overridetoString()
, and thus you retrieve the generalObject#toString()
response:Since: API Level 1
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: