[英]How to make a List Menu with Icon, Title, and a Description to open Another separate activity for each listView item?
I am trying to Develop a custom List menu with a unique Icon on the left a title and a small description underneath the title. 我正在尝试开发一个自定义列表菜单,该菜单的左侧有一个唯一的图标,标题是一个标题,标题下面是一个小说明。 I tried a few variations but none of them seem to work.
我尝试了几种变体,但似乎没有一种起作用。
Here is what I did 这是我所做的
item_view.xml 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>
then I did the main_activity.xml 然后我做了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>
then I did the Countries.java 然后我做了Country.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;
}
}
then I did the MainActivity.java 然后我做了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;
}
}
}
I have a listView with icon on the left, a title, and a description . 我有一个带左侧图标的listView,一个标题和一个description。
My question is how do I make it so when a user clicks on 1 of the list items and opens an individual activity that corresponds to that list item. 我的问题是,当用户单击列表项之一并打开与该列表项相对应的单个活动时,我该如何做。 ????
????
I think you need some modification in your class. 我认为您需要在课堂上进行一些修改。 There can be several way but I think this one will fine for you.
可以有几种方法,但我认为这对您会很好。
Change your MyListAdapter to below code. 将您的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);
}
});
}
}
Change 更改
private void populateListView() {
ArrayAdapter<Countries> adapter = new MyListAdapter(this, myCountries, list);
ListView list = (ListView) findViewById(R.id.countryList);
list.setAdapter(adapter);
}
you can create "setOnItemClickListener" in your main activity also. 您也可以在主要活动中创建“ setOnItemClickListener”。 Only change will be while creating activity "Intent my_activity_intent = new Intent(MainActivity.this, my_activity.class);"
创建活动时只会发生变化“ Intent my_activity_intent = new Intent(MainActivity.this,my_activity.class);” need to do for activity intent.
需要做的活动意图。
If you have any doubt you can ask. 如果您有任何疑问,可以询问。 Its all about basics and your programming style.
它全部与基础知识和您的编程风格有关。 Try to learn such things.
尝试学习这样的东西。 :)
:)
my solution is only applicable if you are having limited number of list items. 仅当您的列表项数量有限时,我的解决方案才适用。
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
}
}
});
And if you are having more number items then another possible solution is to pass all the data of selected item ie image,text,description and display it in next activity. 如果您有更多的项目,那么另一种可能的解决方案是传递所选项目的所有数据,即图像,文本,说明,并在下一个活动中显示它。
You can ask if you have any further problems :) 您可以询问是否还有其他问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.