Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView在单击项目时打开活动?_Android_Listview_Onclick_Android Listview - Fatal编程技术网

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

Android ListView在单击项目时打开活动?,android,listview,onclick,android-listview,Android,Listview,Onclick,Android Listview,我正在尝试构建这个应用程序,它由一个ListView和其他不同的活动组成,当点击视图中的一个项目时,这些活动将被调用。但是,我无法在主活动中实现工作onclicklistener。使用下面的代码,我无法打开活动 代码如下: public void setOnItemClickListener(AdapterView<?> parent, View view, int position, long id) { switch( position ) {

我正在尝试构建这个应用程序,它由一个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 void setOnItemClickListener(AdapterView父视图、视图、,
内部位置,长id){
开关(位置)
{
案例0:Intent newActivity0=新Intent(这是TempuraActivity.class);
startActivity(newActivity0);
打破
案例1:Intent newActivity1=新Intent(this,TempuraActivity.class);
startActivity(新活动1);
打破
案例2:Intent newActivity2=新Intent(this,TempuraActivity.class);
startActivity(新活动2);
打破
案例3:Intent newActivity3=新Intent(this,TempuraActivity.class);
startActivity(新活动3);
打破
案例4:Intent newActivity4=新Intent(this,TempuraActivity.class);
startActivity(新活动4);
打破
}
}
任何帮助将不胜感激:)评论如果你需要看到完整的代码,我会在帖子中更新。谢谢

谢谢您的回复 以下是完整的代码:

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;
        }
     }            
   }
}
public类MainActivity扩展了ListActivity{
私人停车场;
专用矢量数据;
罗维达路;;
静态最终字符串[]标题=新字符串[]{
“圣诞薄饼”、“多拉亚基”、“咸牛肉”、“天妇罗”
};
静态最终字符串[]发布=新字符串[]{
“新的!”,“新的!”,“4个月前”,“新的!”
};
静态最终字符串[]说明=新字符串[]{
“呜呜……”、“帕姆”、“新时代”、“史蒂夫·乔布斯死了”
};
静态最终字符串[]难度=新字符串[]{
“硬”、“容易”、“中等”、“硬”
};
静态最终字符串[]服务=新字符串[]{
“4人”、“2人”、“2杯”、“1人”
};
静态最终字符串[]时间=新字符串[]{
“40分钟”、“20分钟”、“50分钟”、“120分钟”
};
私有整数[]imgid={
拉深,拇指一,拇指二,拇指三,
R.drawable.thumbfour
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater=(LayoutInflater)getSystemService(
活动。布局(充气机和服务);
数据=新向量();

对于(int i=0;i更新

改变

public void setOnItemClickListener(AdapterView<?> parent, View view,
  int position, long id) {
它应该会起作用

原创

您的代码有点混乱,存在一个对象,但实际上需要将该方法传递给一个对象(或匿名类)

也许您正在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;
}              
    }
});
在列表活动中,取消对
getListView()的额外调用


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

更新

改变

public void setOnItemClickListener(AdapterView<?> parent, View view,
  int position, long id) {
它应该会起作用

原创

您的代码有点混乱,存在一个对象,但实际上需要将该方法传递给一个对象(或匿名类)

也许您正在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;
}              
    }
});
在列表活动中,取消对
getListView()的额外调用


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

当您单击时会发生什么?它是否进入开关?它是否崩溃?发布完整的代码,似乎在实现中存在一些问题,例如:代码应该在onItemClick而不是SetOnItemClick Listener中单击时会发生什么?它是否进入开关?它是否崩溃?发布整个代码,在实现上似乎有一些问题,例如:代码应该在onItemClick而不是SetOnItemClick Listeneri中。我改变了你在更新中告诉我的内容,但当我单击时,它强制关闭。但似乎你的方法帮了一点忙。以前单击时什么都没有发生,现在它强制关闭。因此,基本上有一个陷阱w(我想我当时已经回答了你最初的问题(尽管我可能错了)。如果您认为新问题与侦听器的原始问题有关,请发布错误日志。如果没有,请创建一个新问题,因为您的新活动可能会使应用程序崩溃。哦,天哪!我的糟糕!我没有在清单中声明该活动!是的,您的回答很有帮助!非常感谢!非常感谢!我更改了您告诉我的内容n你的更新,但当我点击时,它会强制关闭。但似乎你的方法有点帮助。以前点击时什么都没发生,现在它会强制关闭。所以这一行中基本上有一个陷阱。:(我怀疑我当时已经回答了你最初的问题(尽管我可能错了)。如果您认为新问题与侦听器的原始问题有关,请发布错误日志。如果没有,请创建一个新问题,因为您的新活动可能会使应用程序崩溃。哦,天哪!我的糟糕!我还没有在清单中声明该活动!是的,您的回答很有帮助!非常感谢!非常感谢!