ListView在单击项目时打开活动?

时间:2012-01-05 15:39:02

标签: android listview onclick android-listview

我试图构建这个简单包含ListView的应用程序,以及在单击视图中的项目时将调用的其他活动。但是,我无法在主活动中实现onclicklistener。使用下面的代码,我无法打开活动。

下面是代码:

public void setOnItemClickListener(AdapterView<?> parent, View view,
      int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity4);
                break;
    }
}

任何帮助将不胜感激:)评论如果你需要看到完整的代码,我会在帖子中更新。谢谢!

感谢所有回复! 下面是完整的代码:

public class MainActivity extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;

static final String[] title = new String[] {
    "Christmas Crepes", "Dorayaki", "Corned Beef", "Tempura"
    };

static final String[] release = new String[] {
"New!", "New!", "4 months ago", "New!"
};

static final String[] description = new String[] {
    "Blehh..","Pam pam pam","new era","Steve Jobs is dead"
    };

static final String[] difficulty = new String[] {
"Hard","Easy","Medium","Hard"
};

static final String[] serves = new String[] {
"4 persons","2 persons","2 cups","1 person"
};

static final String[] time = new String[] {
"40 mins","20 mins","50 mins","120 mins"
};

private Integer[] imgid = {
  R.drawable.thumbone,R.drawable.thumbtwo,R.drawable.thumbthree,
  R.drawable.thumbfour
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){

try {
rd = new       RowData(i,title[i],release[i],description[i],difficulty[i],serves[i],time[i]);
    } catch (ParseException e) {
    e.printStackTrace();
   }
   data.add(rd);
   }
   CustomAdapter adapter = new CustomAdapter(this, R.layout.list, android.R.id.list, data);
   setListAdapter(adapter);
   getListView().setTextFilterEnabled(true);
   }
   public void setOnItemClickListener(AdapterView<?> parent, View view,
      int position, long id) {
    switch( position )
    {
       case 0:  Intent newActivity0 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity0);
                break;
       case 1:  Intent newActivity1 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity1);
                break;
       case 2:  Intent newActivity2 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity2);
                break;
       case 3:  Intent newActivity3 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity3);
                break;
       case 4:  Intent newActivity4 = new Intent(this, TempuraActivity.class);     
                startActivity(newActivity4);
                break;
    }
}
   private class RowData {
   protected int mId;
   protected String mTitle;
   protected String mRelease;
   protected String mDescription;
   protected String mDifficulty;
   protected String mServes;
   protected String mTime;
   RowData(int id, String title, String release, String description, String difficulty, String serves, String time){
   mId=id;
   mTitle = title;
   mRelease = release;
   mDescription = description;
   mDifficulty = difficulty;
   mServes = serves;
   mTime = time;
   }
   @Override
   public String toString() {
           return mId+" "+mTitle+" "+mRelease+" "+mDescription+" "+mDifficulty+" "+mServes+" "+mTime;
   }
}
private class CustomAdapter extends ArrayAdapter<RowData> {

public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {               

super(context, resource, textViewResourceId, objects);
}
  @Override
   public View getView(int position, View convertView, ViewGroup parent) {   

   ViewHolder holder = null;
   TextView title = null;
   TextView release = null;
   TextView description = null;
   TextView difficulty = null;
   TextView serves = null;
   TextView time = null;
   ImageView thumbnail=null;
   RowData rowData= getItem(position);
   if(null == convertView){
        convertView = mInflater.inflate(R.layout.list, null);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
}
         holder = (ViewHolder) convertView.getTag();
         title = holder.gettitle();
         title.setText(rowData.mTitle);
         release = holder.getdescription();
         release.setText(rowData.mRelease);
         description = holder.getrelease();
         description.setText(rowData.mDescription);
         difficulty = holder.getdifficulty();
         difficulty.setText(rowData.mDifficulty);
         serves = holder.getserves();
         serves.setText(rowData.mServes);
         time = holder.gettime();
         time.setText(rowData.mTime);

         thumbnail=holder.getImage();
         thumbnail.setImageResource(imgid[rowData.mId]);
         return convertView;
}
        private class ViewHolder {
        private View mRow;
        private TextView title = null;
        private TextView release = null;
        private TextView description = null;
        private TextView difficulty = null;
        private TextView serves = null;
        private TextView time = null;
        private ImageView thumbnail=null; 

        public ViewHolder(View row) {
        mRow = row;
}
    public TextView gettitle() {
         if(null == title){
             title = (TextView) mRow.findViewById(R.id.title);
            }
        return title;
     }     

    public TextView getrelease() {
        if(null == release){
            release = (TextView) mRow.findViewById(R.id.release);
           }
       return release;
    } 

     public TextView getdescription() {
         if(null == description){
              description = (TextView) mRow.findViewById(R.id.description);
                }
       return description;
     }
     public TextView getdifficulty() {
         if(null == difficulty){
             difficulty = (TextView) mRow.findViewById(R.id.difficulty);
            }
        return difficulty;
     }  
     public TextView getserves() {
         if(null == serves){
             serves = (TextView) mRow.findViewById(R.id.serves);
            }
        return serves;
     }  
     public TextView gettime() {
         if(null == time){
             time = (TextView) mRow.findViewById(R.id.title);
            }
        return time;
     }  

    public ImageView getImage() {
         if(null == thumbnail){
              thumbnail = (ImageView) mRow.findViewById(R.id.thumbnail);
                                  }
            return thumbnail;
        }
     }            
   }
}

如果有更好的方法来实现我的应用程序应该,请发布!我迫切需要一次完成这个!

1 个答案:

答案 0 :(得分:4)

<强>更新

更改

public void setOnItemClickListener(AdapterView<?> parent, View view,
  int position, long id) {

protected void onListItemClick(ListView l, View v, int position, long id) {

它应该有用。

<强> ORIGINAL

您的代码有点令人困惑,存在setOnItemClickListener,但您需要实际传递方法onItemClickListener对象(或匿名类)。

也许您正在ListActivity中寻找类似的内容。

final ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt,
            long paramLong) {
        switch( position )
{
   case 0:  Intent newActivity0 = new Intent(YourActivity.this, TempuraActivity.class);     
            startActivity(newActivity0);
            break;
   case 1:  Intent newActivity1 = new Intent(YourActivity.this, TempuraActivity.class);     
            startActivity(newActivity1);
            break;
   case 2:  Intent newActivity2 = new Intent(YourActivity.this, TempuraActivity.class);     
            startActivity(newActivity2);
            break;
   case 3:  Intent newActivity3 = new Intent(YourActivity.this, TempuraActivity.class);     
            startActivity(newActivity3);
            break;
   case 4:  Intent newActivity4 = new Intent(YourActivity.this, TempuraActivity.class);     
            startActivity(newActivity4);
            break;
}              
    }
});

在这里,您可以在onItemClickonItemClickListener方法中实现您的switch语句,它应该有效。

注意:您必须将this更改为YourActivity.this,因为您将进入内部类,并且需要引用外部活动类。

或者,您可以覆盖onListItemClick

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // ... put switch statement here
}

在列表活动中删除对getListView()

的额外调用

如果这不能回答您的问题请完整代码