如何根据listview项目点击启动活动?

时间:2015-08-29 12:45:24

标签: java android listview android-listview onclick

我有以下代码,但我已经尝试了几天,我似乎无法弄清楚如何基于单击的listview项启动活动,结果[position]不适用于启动意图。

public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
    // TODO Auto-generated constructor stub
    result=prgmNameList;
    context=mainActivity;
    imageId=prgmImages;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

编辑:如果单击第一个列表项,我希望它启动一个活动(图像类),如果第二个单击以启动images2类等等。

4 个答案:

答案 0 :(得分:2)

在适配器中未完成基于ListView的项目单击启动活动。它在您显示ListView的活动中完成:

在您的活动中:

mListView.setAdapter(mAdapter); //mAdapter is your CustomAdapter 
mListView.setClickable(true);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Start the activity here.
        //based on the value of position or id.
    }
});

答案 1 :(得分:1)

请在onclick上试试

    if(position ==1){
    --fire your first intent here.--
    } else if(position ==2) {
    -------your other intent------
    } 
    ----etc

答案 2 :(得分:0)

不要将位置作为getView()中的最终静态参数,这是一种冤枉方式。使用下面的getView方法从列表项

启动StartActivity
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.program_list, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textView1);
    holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setTag(position);
    rowView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (Integer) v.getTag;
            Intent i = new Intent(context, YourActivity.class);
            context.startActivity(i);
        }
    });
    return rowView;
}

答案 3 :(得分:0)

尝试这样的事情

rowView.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
         Intent intent=new Intent(v.getContext(),ActivityYouWantToGo.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         v.getContext().startActivity(intent);
         Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
    }
});

修改

只需在BaseAdapter

上创建此方法即可
public void StartActivityForImages(Context context, int i){

    switch (i){
        case 0:
            Intent Intent = new Intent(context, Image0.class);
            context.startActivity(Intent);
            break;

        case 1:
            Intent Intent2 = new Intent(context, Image1.class);
            context.startActivity(Intent2);
            break;
        case 2:
            Intent Intent3 = new Intent(context, Image3.class);
            context.startActivity(Intent3);
            break;

        ......
    }

}

setOnClickListener()

您可以按如下方式调用此方法:

 StartActivityForImages(v.getContext(),position);
            Toast.makeText(context, "You Clicked " + result[position], Toast.LENGTH_LONG).show();

有效的方法

如果您不想创建方法并执行此操作,您可以执行以下操作:

使用下一个类的名称创建一个String,如:

String NextActivity = getPackageName()+".Image"+position;

然后你可以这样做:

try {
      String className =getPackageName()+".Image"+2;
      Intent openNewIntent = new Intent(v.getContext(), Class.forName( className ) );
      startActivity(  openNewIntent );
 } catch (ClassNotFoundException e) {
         Log.d("ERRORPEW", e.getMessage());
         e.printStackTrace();
 }