Java ListView-McClick
我正在使用arrayadapter在我的listview中显示图片、标题和说明Java ListView-McClick,java,android,listviewitem,Java,Android,Listviewitem,我正在使用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_ord
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);
lv1.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple_list_item_1,lv_arr))*/
m_orders=new ArrayList();
this.m_adapter=新订单适配器(this,R.layout.row,m_订单);
最终ListView lv1=(ListView)findViewById(R.id.list);
lv1.setAdapter(这个.m_适配器);
lv1.setTextFilterEnabled(true);
现在我想让onclick获得冠军
我试过这个,但它显示出奇怪的字符
编辑:这是我的班级:
私有类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();
}
});
私有数组列表项;
public OrderAdapter(上下文上下文、int textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.行,空);
}
订单o=项目。获取(位置);
如果(o!=null){
TextView tt=(TextView)v.findViewById(R.id.toptext);
TextView bt=(TextView)v.findViewById(R.id.bottomtext);
如果(tt!=null){
tt.setText(“名称:+o.getOrderName());}
如果(bt!=null){
bt.setText(“状态:+o.getOrderStatus());
}
}
返回v;
}
}
lv1.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
AlertDialog.Builder adb=新建AlertDialog.Builder(ListViewExampleActivity.this);
adb.设置标题(“所选项目”);
//lv1.getItemAtPosition(位置)
adb.setMessage(“所选项目为=“+lv1.getItemAtPosition(position).toString());
adb.setPositiveButton(“确定”,空);
adb.show();
}
});
那么如何指定要显示的项目,如arraylist(0,1)。。。
关于…而不是
lv1.getItemAtPosition(position).toString()
尝试
a、 getItemAtPosition(position).toString()代替
lv1.getItemAtPosition(position).toString()
尝试
a、 getItemAtPosition(position).toString()您正在为同一个
列表视图设置两个适配器:首先是ArrayAdapter
,然后是OrderAdapter
(无论您如何定义)
由于最后一个是OrderAdapter
,getItemAtPosition(位置)
检索类顺序的实例(或在该适配器中使用的任何对象),而不是字符串。最有可能的是,该类不会重写toString()
,因此您可以检索常规对象#toString()
响应:
public String toString ()
自:API级别1
返回一个字符串,其中包含此对象的简明易读描述。鼓励子类重写此方法,并提供考虑对象类型和数据的实现。默认实现相当于以下表达式:
getClass().getName() + '@' + Integer.toHexString(hashCode())
如果您打算实现自己的toString方法,请参阅编写一个有用的toString方法。
返回此对象的可打印表示形式
编辑:澄清一点我的答案:问题不在听众内部。看起来不错。您将需要研究如何设置适配器以及如何(如果)定义任何类。例如,创建您自己的toString()方法以返回任何有意义的字符串
根据您的更新信息:如果我没弄错,您想做的是:
adb.setMessage("Selected Item is = "+ lv1.getItemAtPosition(position).getOrderName());
您正在将两个适配器设置为相同的列表视图
:首先是阵列适配器
,然后是顺序适配器
(不管您如何定义)
由于最后一个是OrderAdapter
,getItemAtPosition(位置)
检索类顺序的实例(或在该适配器中使用的任何对象),而不是字符串。最有可能的是,该类不会重写toString()
,因此您可以检索常规对象#toString()
响应:
public String toString ()
自:API级别1
返回一个字符串,其中包含此对象的简明易读描述。鼓励子类重写此方法,并提供考虑对象类型和数据的实现。默认实现相当于以下表达式:
getClass().getName() + '@' + Integer.toHexString(hashCode())
如果您打算实现自己的toString方法,请参阅编写一个有用的toString方法。
返回此对象的可打印表示形式
编辑:澄清一点我的答案:问题不在听众内部。看起来不错。您将需要研究如何设置适配器以及如何(如果)定义任何类。例如,创建您自己的toString()方法以返回任何有意义的字符串
根据您的更新信息:如果我没弄错,您想做的是:
adb.setMessage("Selected Item is = "+ lv1.getItemAtPosition(position).getOrderName());
感谢您的回答,这是我的orderadapter类:感谢您的回答,这是我的orderadapter类: