如何创建带有图标,标题和描述的列表菜单以打开每个listView项目的另一个单独活动?

时间:2013-12-26 04:01:29

标签: java android

我正在尝试开发一个自定义列表菜单,左侧是标题,标题下方有一个小描述。我尝试了一些变化,但似乎都没有。

这就是我做的事情

item_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="10dp"
android:paddingBottom="15dp" >

<ImageView
    android:id="@+id/item_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/union_europea" />

<TextView
    android:id="@+id/year"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/item_icon"
    android:layout_toRightOf="@+id/item_icon"
    android:text="Small Text"
    android:paddingLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/countryName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/year"
    android:layout_alignParentTop="true"
    android:text="Large Text"
    android:textSize="18dp"
    android:paddingLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/continent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:paddingRight="10dp"
    android:textSize="12dp"
           android:paddingLeft="10dp"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

然后我做了 main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MainActivity" >

<ListView
    android:id="@+id/countryList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

然后我做了Countries.java

public class Countries {
private String country;
private int year;
private int iconID;
private String continent;

public Countries (String country, int year, int iconID, String continent){
    super();
    this.country = country;
    this.year = year;
    this.iconID = iconID;
    this.continent = continent;
}
public String getCountry() {
    return country;
}
public int getYear() {
    return year;
}
public int getIconID() {
    return iconID;
}
public String getContinent() {
    return continent;
}   
}

然后我做了 MainActivity.java

public class MainActivity extends Activity {

private List<Countries> myCountries = new ArrayList<Countries>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    populateCountryList();
    populateListView();
}


private void populateCountryList() {

    myCountries.add(new Countries("European Union", 2014, R.drawable.union_europea, "Europe"));
    myCountries.add(new Countries("Spain", 2015, R.drawable.espania, "Europe"));
    myCountries.add(new Countries("Finland", 2016, R.drawable.finlandia, "Europe"));
    myCountries.add(new Countries("France ", 2017, R.drawable.francia, "Europe"));
    myCountries.add(new Countries("Ireland ", 2018, R.drawable.irlanda, "Europe"));
    myCountries.add(new Countries("Italy", 2014, R.drawable.italia, "Europe"));
    myCountries.add(new Countries("Monaco ", 2014, R.drawable.monaco, "Europe"));
    myCountries.add(new Countries("Portugal", 2014, R.drawable.portugal, "Europe"));
    myCountries.add(new Countries("Russia", 2014, R.drawable.rusia, "Europe"));
    myCountries.add(new Countries("Malta", 2014, R.drawable.malta, "Europe"));


}


private void populateListView() {
    ArrayAdapter<Countries>  adapter = new MyListAdapter();
    ListView list = (ListView) findViewById(R.id.countryList);
    list.setAdapter(adapter);
}




private class MyListAdapter extends ArrayAdapter<Countries>{
    public MyListAdapter(){
        super(MainActivity.this, R.layout.item_view, myCountries);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View itemView = convertView;
        // make sure we have a view to work with
        if(itemView == null){
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
        }


        // find country

        Countries currentCountry = myCountries.get(position);

        // fill the view
        ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
        imageView.setImageResource(currentCountry.getIconID());

        TextView countryText = (TextView) itemView.findViewById(R.id.countryName);
        countryText.setText(currentCountry.getCountry());

        TextView yearText = (TextView) itemView.findViewById(R.id.year);
        yearText.setText("" + currentCountry.getYear());

        TextView continentText = (TextView) itemView.findViewById(R.id.continent);
        continentText.setText(currentCountry.getContinent());   

        return itemView;
    }
}
}

我有一个带左侧图标的listView,一个标题和一个描述。

我的问题是当用户点击其中一个列表项并打开与该列表项对应的单个活动时,我该如何实现。 ????

2 个答案:

答案 0 :(得分:0)

我认为您需要在课堂上进行一些修改。可以有几种方式,但我认为这个对你很好。

将MyListAdapter更改为以下代码。

    public MyListAdapter(Context context, List<Countries> country_list, ListView list_view) {
        super(MainActivity.this, R.layout.item_view, myCountries);

        if (list_view != null) {
            list_view.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {

                    // YOU CAN START YOUR ACTIVITY HERE.
                    Intent my_activity_intent = new Intent(context, my_activity.class);
                    startActivity(my_activity_intent);
                }
            });
        }
    }

更改

private void populateListView() {
    ArrayAdapter<Countries>  adapter = new MyListAdapter(this, myCountries, list);
    ListView list = (ListView) findViewById(R.id.countryList);
    list.setAdapter(adapter);
}

您也可以在主活动中创建“setOnItemClickListener”。只有在创建活动时才会进行更改“Intent my_activity_intent = new Intent(MainActivity.this,my_activity.class);”需要做的是活动意图。

如果您有任何疑问,可以提问。它完全基于基础知识和编程风格。尝试学习这些东西。 :)

答案 1 :(得分:0)

我的解决方案仅适用于列表项数量有限的情况。

list_view.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                         switch(arg2)
                         {
                           case 0:
                                startActivity(new Intent(context,Activity0.class));
                                break;
                            case 1:
                                startActivity(new Intent(context,Activity1.class));
                                break;
                           //Implement cases for all list items

                         } 



                }
            });

如果您有更多的数字项目,那么另一种可能的解决方案是传递所选项目的所有数据,即图像,文本,描述,并将其显示在下一个活动中。

您可以询问是否还有其他问题:)