将活动添加到ListView

时间:2016-03-02 18:06:32

标签: android listview

我想创建一个ListView,其项目在第二个活动中打开不同的图片(当它们被选中时)。

我已经做了一些但是无法弄清楚如何编写代码以便在第二个活动中显示不同的图片。

感谢任何帮助。

这是我到目前为止所做的:

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView listView;

String[] country = {

        "India",
        "France",
        "Germany",
        "Japan",
        "The US",
        "Spain",
        "Brazil",
        "Korea"
};

Integer[] countryFlag = {

        R.drawable.pic1,
        R.drawable.pic2,
        R.drawable.pic3,
        R.drawable.pic4,
        R.drawable.pic5,
        R.drawable.pic6,
        R.drawable.pic7,
        R.drawable.pic8,
};

@Override
protected void onCreate(Bundle SavedInstanceState) {

    super.onCreate(SavedInstanceState);

    setContentView(R.layout.activity_main);


    listView = (ListView) findViewById(R.id.listView);

    CustomListAdapter adapter = new CustomListAdapter(this, country, countryFlag);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



            Intent intent = new Intent(MainActivity.this, NextScreen.class);

            intent.putExtra("position", position);
            intent.putExtra("country", country);
            intent.putExtra("countryFlag", countryFlag);

            startActivity(intent);

        }
    });

}
}

CustomListAdapter.java

public class CustomListAdapter extends ArrayAdapter<String> {

Activity context;
String[] country;
Integer[] countryFlag;

static class ViewHolder{
    public ImageView img;
    public TextView txt;
}

public CustomListAdapter(Activity context, String[] country, Integer[] countryFlag) {
    super(context, R.layout.second_layout, country);

    this.context=context;
    this.country=country;
    this.countryFlag=countryFlag;
}

public View getView (int position, View view, ViewGroup parent) {
    View row = view;

    if (row == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        row = inflater.inflate(R.layout.second_layout, parent, false);

        ViewHolder viewHolder = new ViewHolder();

        viewHolder.img = (ImageView) row.findViewById(R.id.img);
        viewHolder.txt = (TextView) row.findViewById(R.id.txt);

        row.setTag(viewHolder);
    }

    ViewHolder holder = (ViewHolder) row.getTag();

    holder.txt.setText("The country of " + country[position]);
    holder.img.setImageResource(countryFlag [position]);

    return row;
}
}

NextScreen.java

公共类NextScreen扩展了Activity {

String[] position;
String[] country;
int countryFlag;


@Override
protected void onCreate(Bundle SavedInstanceState) {

    super.onCreate(SavedInstanceState);

    setContentView(R.layout.third_layout);

    Intent intent = getIntent();

    position = intent.getStringArrayExtra("position");
    country = intent.getStringArrayExtra("country");
    countryFlag = intent.getExtras().getInt("countryFlag");

}
}

1 个答案:

答案 0 :(得分:1)

你在意图中传递和接收额外内容的方式是错误的。你正在传递一些东西并阅读其他内容。

String[] country;
int[] countryFlag;
int position;


@Override
protected void onCreate(Bundle SavedInstanceState) {

    super.onCreate(SavedInstanceState);

    setContentView(R.layout.third_layout);
    //assuming you have an image view in the layout with id flag
    ImageView flagImage = (ImageView)findViewById(R.id.flag);
    Intent intent = getIntent();

    country = intent.getStringArrayExtra("country");
    countryFlag = intent.getIntArrayExtra("countryFlag");
    position = intent.getExtras().getInt("position");
    int imageID = countryFlag[position];
    //now you can use this imageId to set on a imageView
    flagImage.setImageResource(imageID);
}