Java 带有字符串和图像的ListView
我希望ListView显示一个字符串,然后图像和字符串再次显示在同一行中,但它在图像的左侧和右侧显示相同的字符串。如何解决此问题。我仍在学习java,无法理解它 主要活动Java 带有字符串和图像的ListView,java,android,string,listview,Java,Android,String,Listview,我希望ListView显示一个字符串,然后图像和字符串再次显示在同一行中,但它在图像的左侧和右侧显示相同的字符串。如何解决此问题。我仍在学习java,无法理解它 主要活动 public class MainActivity extends ActionBarActivity { String color_names[] = {"red", "green", "blue", "yellow", "pink", "brown"}; Integer image_id[] = {R.drawable.
public class MainActivity extends ActionBarActivity {
String color_names[] = {"red", "green", "blue", "yellow", "pink", "brown"};
Integer image_id[] = {R.drawable.red, R.drawable.green, R.drawable.blue, R.drawable.yellow, R.drawable.pink, R.drawable.brown};
String number_list[] = {"one", "two", "three", "four", "five", "six"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Customlistadapter adapter = new Customlistadapter(this, image_id, color_names, number_list);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
自定义列表适配器
public class Customlistadapter extends ArrayAdapter{
String[] color_names;
String[] number_list;
Integer[] image_id;
Context context;
public Customlistadapter(Activity context, Integer[] image_id, String[] color_names, String[] text){
super(context, R.layout.list_row, text);
// TODO Auto-generated constructor stub
this.color_names = text;
this.image_id = image_id;
this.number_list = text;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View single_row = inflater.inflate(R.layout.list_row, null,
true);
TextView textView = (TextView) single_row.findViewById(R.id.textView);
ImageView imageView = (ImageView) single_row.findViewById(R.id.imageView);
TextView textView1 = (TextView) single_row.findViewById(R.id.textList);
textView.setText(color_names[position]);
imageView.setImageResource(image_id[position]);
textView1.setText(number_list[position]);
return single_row;
}
}
罗列
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center_vertical"
android:text="TextView" />
<ImageView
android:id="@+id/imageView"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_gravity="center_vertical"
android:text="TextView1" />
</LinearLayout>
中的
你应该有this.color\u names=color\u names
而不是this.color\u names=text代码>
基本上,字符串数组包含相同的值,这就是为什么图像旁边有相同的字符串
您还应该了解视图持有者模式以及如何使用convertView
您应该了解java命名约定。你应该使用camelcase。例如Customlistadapter->Customlistadapter、颜色名称->颜色名称等
public Customlistadapter(Activity context, Integer[] image_id, String[] color_names, String[] text){
super(context, R.layout.list_row, text);
// TODO Auto-generated constructor stub
this.color_names = text;
this.image_id = image_id;
this.number_list = text;
this.context = context;
}