0

Bah, what am i doing wrong here I've set the onItemClickListener for the list view and the onClick to take it to different activities but it's telling me it must use the method OnItemClicklistener and i have...

package org.iimed.www;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.iimed.www.R;
import org.iimed.www.CustomListViewAdapter;
import org.iimed.www.RowItem;

public class ImageTextListViewActivity extends Activity implements
        OnItemClickListener {

    public static final String[] titles = new String[] { "Penicillins",
            "Cephalosporins", "Monobactams", "Carbapenems","Macrolides","Tetracyclines",};

    public static final String[] descriptions = new String[] {
            "It is an aggregate accessory fruit",
            "It is the largest herbaceous flowering plant", "Citrus Fruit",
            "Mixed Fruits","none","none" };

    public static final Integer[] images = { R.drawable.antibiotic_buton,
            R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };

    ListView listView;
    List<RowItem> rowItems;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screj);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
            rowItems.add(item);


        listView = (ListView) findViewById(R.id.list);
        CustomListViewAdapter adapter = new CustomListViewAdapter(this,
                R.layout.list_item, rowItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

               @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                   {
             Intent intent = null ;
                switch (arg2) {
            case 0:
                intent  = new Intent(ImageTextListViewActivity.this, MainActivity.class);
                startActivity(intent);          
                break;
            case 1:
            intent = new Intent(ImageTextListViewActivity.this, Miipet.class);
            startActivity(intent);
                break;
            default:
                break;



                }}}}

The adapter :

package org.iimed.www;


import java.util.List;
import org.iimed.www.R;
import org.iimed.www.RowItem;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

   Context context;

   public CustomListViewAdapter(Context context, int resourceId,
           List<RowItem> items) {
       super(context, resourceId, items);
       this.context = context;
   }


   private class ViewHolder {
       ImageView imageView;
       TextView txtTitle;
       TextView txtDesc;
   }

   public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder = null;
       RowItem rowItem = getItem(position);

       LayoutInflater mInflater = (LayoutInflater) context
               .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
       if (convertView == null) {
           convertView = mInflater.inflate(R.layout.list_item, null);
           holder = new ViewHolder();
           holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
           holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
           holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
           convertView.setTag(holder);
       } else
           holder = (ViewHolder) convertView.getTag();

       holder.txtDesc.setText(rowItem.getDesc());
       holder.txtTitle.setText(rowItem.getTitle());
       holder.imageView.setImageResource(rowItem.getImageId());

       return convertView;
   }
}
4

2 回答 2

2

像这样点击注册:

 lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
            // TODO Auto-generated method stub

             Intent intent = new Intent(getApplicationContext(), ImageGridActivity.class);

              startActivity(intent);
        }
    });
于 2013-10-24T06:11:30.857 回答
1

我检查了你的代码,有几个问题

1)您循环浏览元素并为每个元素创建适配器

2)你在onCreate里面实现了onItemClick

我粘贴了您的代码的修订版,但我已经用 Object 更改了 RowItem 并用 ListAdapter 更改了 CustomListViewAdapter 所以在那里使用你的对象,但这应该是你的代码的结构:)

package org.iimed.www;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class ImageTextListViewActivity extends Activity implements OnItemClickListener {

public static final String[] titles = new String[] { "Penicillins",
    "Cephalosporins", "Monobactams",      "Carbapenems","Macrolides","Tetracyclines",};

public static final String[] descriptions = new String[] {
    "It is an aggregate accessory fruit",
    "It is the largest herbaceous flowering plant", "Citrus Fruit",
    "Mixed Fruits","none","none" };

public static final Integer[] images = { R.drawable.antibiotic_buton,
    R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,   R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };

ListView listView;
List<Object> rowItems;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screj);

    rowItems = new ArrayList<Object>();
    for (int i = 0; i < titles.length; i++) {
        Object item = new Object();
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    ListAdapter adapter = new ListAdapter(this,R.layout.list_item, rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Intent intent = null ;
    switch (arg2) {
    case 0:
        intent  = new Intent(ImageTextListViewActivity.this, MainActivity.class);
        startActivity(intent);          
        break;
    case 1:
        intent = new Intent(ImageTextListViewActivity.this, Miipet.class);
        startActivity(intent);
        break;
    default:
        break;
    }
}
}
于 2013-10-24T06:20:08.310 回答