下面是我的listactivity代码,任何人都可以告诉我如何获得项目点击事件?
public class MyCustomListView extends ListActivity
{
private ArrayList<String> m_orders=null;
private ArrayList<String> artistsList=new ArrayList<String>();
private OrderAdapter m_adapter;
String[] array;
String[] artists;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
Intent intent=getIntent();
array=intent.getStringArrayExtra("trackname");
artists=intent.getStringArrayExtra("artists");
m_orders = new ArrayList<String>();
for(int i=0;i<array.length;i++)
{
m_orders.add(array[i]);
artistsList.add(artists[i]);
}
this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
setListAdapter(this.m_adapter);
}
private class OrderAdapter extends BaseAdapter
{
private ArrayList<String> items;
public OrderAdapter(Context context,int id,ArrayList<String> items)
{
super();
this.items=items;
}
public View getView(int position,View convertView,ViewGroup parent)
{
View view=convertView;
if(view==null)
{
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.row,parent,false);
}
String string=items.get(position);
if(string!=null)
{
TextView t1=(TextView)view.findViewById(R.id.toptext);
TextView t2=(TextView)view.findViewById(R.id.bottomtext);
if(t1!=null)
{
t1.setText(items.get(position));
}
if(t2!=null)
{
String artistName=artistsList.get(position);
if(artistName.equals(""))
{
t2.setText("UnKnown");
}
else
{
t2.setText(artistName);
}
}
}
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return array.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
@Override
public void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {
Log.i("Position",""+arg2);
}
这段代码有什么问题?
答案 0 :(得分:0)
下面的代码解决了我的问题\;
private class OrderAdapter extends BaseAdapter implements OnClickListener
{
private ArrayList<String> items;
public OrderAdapter(Context context,int id,ArrayList<String> items)
{
super();
this.items=items;
}
public View getView(int position,View convertView,ViewGroup parent)
{
View view=convertView;
if(view==null)
{
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.row,parent,false);
}
String string=items.get(position);
if(string!=null)
{
TextView t1=(TextView)view.findViewById(R.id.toptext);
TextView t2=(TextView)view.findViewById(R.id.bottomtext);
if(t1!=null)
{
t1.setText(items.get(position));
}
if(t2!=null)
{
String artistName=artistsList.get(position);
if(artistName.equals(""))
{
t2.setText("UnKnown");
}
else
{
t2.setText(artistName);
}
}
view.setOnClickListener(new OnItemClickListener(position));
}
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return array.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void onClick(View v)
{
Log.i("@@@@@@@@@@@@@@","buttonClicked");
}
}
private class OnItemClickListener implements OnClickListener
{
private int mPosition;
OnItemClickListener(int position)
{ mPosition = position; }
@Override
public void onClick(View arg0)
{
Log.v("****************", "onItemClick at position" + mPosition); }
}
答案 1 :(得分:0)
在您的初始代码段中,您忘记设置OnItemClickListener - 在您自己的答案中,您使用view.setOnClickListener(new OnItemClickListener(position));
如果我理解正确,你就是在每个单元格上创建和设置它 - 在ListView本身的层次上进行它只会有一次更有意义。这对于性能和内存消耗应该更好(new OnItemClickListenr()
每次都会创建一个新实例。)