我希望ListView再次显示一个String,然后再显示Image和String,但是它在Image的左侧和右侧显示相同的String我该如何解决这个问题。我还在学习java,无法弄清楚。
MainActivity
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);
}
}
Customlistadapter
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;
}
}
list_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>
答案 0 :(得分:0)
在
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;
}
您应该this.color_names = color_names;
而不是this.color_names = text;
基本上,您的字符串数组包含相同的值,这就是图像旁边有相同字符串的原因。
您还应该查看视图持有者模式以及如何使用convertView http://developer.android.com/training/improving-layouts/smooth-scrolling.html
你应该研究一下java命名约定。你应该使用camelcase。例如Customlistadapter - &gt; CustomListAdapter,color_names - &gt; colorNames等......