制作自定义ListView的onClickListener

时间:2014-02-01 17:00:33

标签: android listview android-listview onclick onclicklistener

我已经实施了自定义列表视图&我在创建onClickListener时面临问题。 在OnCreate()

lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(MainActivity.this,q));

&安培; CustomAdapter类:

 class CustomAdapter extends ArrayAdapter<GS>
  {
       ArrayList<GS> list;
       LayoutInflater mInfalter;    
       public CustomAdapter(Context context, ArrayList<GS> list)
       {
           super(context,R.layout.customlayout,list);
          this.list= list;  
          mInfalter = LayoutInflater.from(context);
        for(int i=0;i<list.size();i++)
        {
            Log.i("................",""+list.get(i).getAS_name());
        }
       }
}
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          Log.i("..........","Hello in getView");
          if(convertView==null)
          {
               convertView = mInfalter.inflate(R.layout.customlayout,parent,false);
               holder = new ViewHolder();
               holder.tv1 = (TextView)convertView.findViewById(R.id.textView1); 
               convertView.setTag(holder); 
          }else{
                holder = (ViewHolder)convertView.getTag();
          } 

                holder.tv1.setText(list.get(position).getAS_name());

          return convertView;
    }

  }

 static class ViewHolder
    {
        TextView tv1;
    }  

它的OnClickListener是否必须在getView()方法中生成? 或者它可以在OnCreate()中制作? 怎么样?

由于

1 个答案:

答案 0 :(得分:1)

这是一个独立的例子,我刚刚放在一起并经过测试,我希望能帮到你:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<MyCustomAdapterItem> items = new ArrayList<MyCustomAdapterItem>();
        items.add(new MyCustomAdapterItem("1", "one"));
        items.add(new MyCustomAdapterItem("2", "two"));

        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, items));
        listView.setOnItemClickListener(this);
    }

    private class MyAdapter extends ArrayAdapter<MyCustomAdapterItem> {
        public MyAdapter(Context context, int textViewResourceId, List<MyCustomAdapterItem> objects) {
            super(context, textViewResourceId, objects);
        }
    }

    private class MyCustomAdapterItem {
        private String id;
        private String text;

        public MyCustomAdapterItem(String id, String text) {
            this.id = id;
            this.text = text;
        }

        public String getText() {
            return text;
        }

        public String getId() {
            return id;
        }

        @Override
        public String toString() {
            return getText();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        MyCustomAdapterItem myCustomAdapterItem = (MyCustomAdapterItem) adapterView.getItemAtPosition(position);
        if ("1".equals(myCustomAdapterItem.getId())) {
            Toast.makeText(this, "I can't believe you just selected this item!!!", Toast.LENGTH_SHORT).show();
        } else if ("2".equals(myCustomAdapterItem.getId())) {
            Toast.makeText(this, "This item is better than the first", Toast.LENGTH_SHORT).show();
        }
    }
}

请注意,在此示例中,Activity实现了AdapterView OnItemClickListener接口。每当您单击列表中的任何项目时,都会调用onItemClick方法。只需确保将活动设置为OnItemClickListener,否则将不会调用onItemClick:

listView.setOnItemClickListener(this)

此onItemClick实现演示了一种可以有条件地执行不同逻辑的方法,具体取决于所单击的项目。如果你提前知道固定位置指数,你可以选择使用position参数。