Listview onclick问题

时间:2015-05-02 10:54:28

标签: android listview

点击列表视图时出现问题。当我点击按钮时没有任何反应。我必须开始一个新的活动。

我想知道我的代码有什么问题:

进口

    public class CountriesListAdapter extends         ArrayAdapter<String> {
    private final Context context;
    private final String[] values;
    ListView listView ;

    public CountriesListAdapter(Context context,      String[] values) {
    super(context, R.layout.country_list_item, values);
    this.context = context;
    this.values = values;
 }

    @Override
    public View getView(int position, View     convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtViewCountryName);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String g=values[position];
    textView.setText((g).trim());
    if (g.equals("1")) {
        imageView.setImageResource(R.drawable.ger);     
    } else if (g.equals("2")) {
        imageView.setImageResource(R.drawable.ukra);
    } else if (g.equals("3")) {
            imageView.setImageResource(R.drawable.k);
    } else if (g.equals("4")) {
        imageView.setImageResource(R.drawable.wf);  
    }
    return rowView;}
         protected void onListItemClick(int position, View convertView, ViewGroup parent) {
             String g=values[position];
                if (g.equals("4")){
                    Intent intent = new Intent(context, Spain.class);
                    context.startActivity(intent);



                }}
 }

1 个答案:

答案 0 :(得分:0)

你必须从列表视图添加的活动或片段中调用onitemclick监听器...

例如......

 listView = (ListView) findViewById(R.id.custom_list);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
           //your code to go to the next activity is here...
        }
    });