将每个ListViewItem显示给另一个不同的活动

时间:2014-04-20 11:22:15

标签: android listview

我有一个带有ImageIcon和Title的简单列表视图。 现在每当我点击我的listItem时,我需要将此listitem发送到另一个活动。像Activity1,Activity2,Activity3等...... 我已阅读了许多SO答案,但无法找到更好的解决方案。 我的主要活动:

    public class MainActivity extends Activity implements
 AdapterView.OnItemClickListener {

ListView list;
String [] memeTitles;
String [] memeDescriptions;
int [] images = {R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Resources res = getResources();
    memeTitles = res.getStringArray(R.array.titles);

    list = (ListView)findViewById(R.id.listView1);
    VivzAdapter adapter = new VivzAdapter(this, memeTitles, images,     memeDescriptions);
    list.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    // TODO Auto-generated method stub


    // i created this activity for first listview item but it's not working
        // other Activity class name = Activity1

        Intent i = new Intent(this, Activity1.class);
        startActivity(i);
  }
  }
   class VivzAdapter extends ArrayAdapter<String>
     {
Context context;
String[] titleArray;

int [] images;
VivzAdapter(Context c, String[] titles, int imgs[], String[] desc )
{
    super(c, R.layout.single_row, R.id.textView1, titles);
    this.context= c;
    this.images=imgs;
    this.titleArray= titles;
//  this.descriptionArray = desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row  = convertView ;
    if (row == null)
    {
    LayoutInflater inflater =  (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.single_row, parent, false);
    }
    ImageView myImage = (ImageView)    row.findViewById(R.id.imageView1);
    TextView  myTitles = (TextView) row.findViewById(R.id.textView1);

    myImage.setImageResource(images[position]);
    myTitles.setText(titleArray[position]); 
    return row;
    }
  }

3 个答案:

答案 0 :(得分:0)

您需要在您的意图中设置值: -

Intent i = new Intent(this, Activity1.class);
i.putExtra("image", images[position]);
i.putExtra("title", titleArray[position]);
startActivity(i);

并在您的其他活动中检索: -

Bundle extras = getIntent().getExtras();
if(extras != null) {
    String titlenewString= extras.getString("title");
    int imageName = extras.getInt("image");
}

答案 1 :(得分:0)

你确实可以把不同的属性放在Rahul建议的不同领域。

另一种可以发送整个视图的方法是创建一个自定义View,它扩展了ListItem的View并实现Parcable。你可以把它放在你的意图中putParcable(String key, Parcable object)

答案 2 :(得分:0)

只需调用list.setOnItemClickListener(this); ,你没有设置听众那就是为什么它不起作用。