所以我想要做的是为我的应用程序中的每个列表项设置不同的图像。我一直试图这样做一段时间,并且不知道我在做什么。
这是我正在尝试做的事情:
我该如何解决这个问题?这是我目前的代码:
package net.androidbootcamp.gamesandcoffee;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[ ] attraction = {"Games", "Coffee Shops"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, R.id.games, attraction));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
startActivity(new Intent(MainActivity.this, games.class));
break;
case 1:
startActivity(new Intent(MainActivity.this, coffee.class));
break;
}
}
}
和我的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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/games"
android:textSize="20sp"
android:text="@+id/games"
android:drawableLeft="@drawable/games"/>
</RelativeLayout>
答案 0 :(得分:2)
为此,您必须创建自定义适配器,您可以在其中自定义所有Object
项。
ListView
答案 1 :(得分:0)
将图像资源ID加载到数组并以这种方式传递给列表适配器
int[] images = {R.drawables.firstimg,R.drawables.secondimg};
setListAdapter(new CustomAdapter(this,images, attraction));
Crate CustomAdapter Class
public class CustomAdapter extends BaseAdapter{
private Context context;
int[] images;
String[] textdata;
public CustomAdapter(Context context, int[] images,String[] textdata) {
this.context = context;
this.images=images;
this.textdata=textdata;
}
@Override
public Object getItem(int position) {
return textdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.drawer_listitem,parent,false);
}else {
row = convertView;
}
TextView name = (TextView) row.findViewById(R.id.draweritemtext);
ImageView image = (ImageView) row.findViewById(R.id.draweritemimage);
name.setText(textdata[position]);
image.setImageResource(images[position]);
return row;
}
@Override
public int getCount() {
return textdata.size();
}
}
答案 2 :(得分:0)
按照这两个视频教程 面向初学者 ,我能够达到我想要的结果。感谢那些贡献者,但所提供的答案或资源都没有针对那些缺乏经验的初学者。
这些是我看过的视频
Android Studio Tutorial - 18 - ListView with Custom Adapter part-1
Android Studio Tutorial - 19 - ListView with Custom Adapter part-2