我遇到了多列表列表視圖的問題。問題是當我向上或向下滾動列表中的主題更改行。 這是我的活動:多列表視圖錯誤視圖
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> temp = null; //new HashMap<String, String>();
//TRASFERISCO I DATI DAGLI ARRAY ALLA MAPPA
for(int i = 0 ; i < 24 ; i++) {
temp = new HashMap<String, String>();
temp.put(FIRST_COLUMN, i + " " + Array_Nome[i]);
temp.put(SECOND_COLUMN, Array_Cognome[i]);
temp.put(THIRD_COLUMN, Array_Cellulare[i]);
temp.put(FOURTH_COLUMN, Array_UUID[i]);
list.add(temp);
}
ListView listView=(ListView)findViewById(R.id.listView1);
MultiColoumnAdapter adapter=new MultiColoumnAdapter(this, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
int pos=position+1;
Toast.makeText(MainActivity.this, Integer.toString(pos)+" Clicked", Toast.LENGTH_SHORT).show();
}
});
這是適配器:
public class MultiColoumnAdapter extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
public MultiColoumnAdapter(Activity activity, ArrayList<HashMap<String, String>> list){
super();
this.activity=activity;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=activity.getLayoutInflater();
if(convertView == null){
convertView=inflater.inflate(R.layout.colmn_row, null);
txtFirst=(TextView) convertView.findViewById(R.id.name);
txtSecond=(TextView) convertView.findViewById(R.id.gender);
txtThird=(TextView) convertView.findViewById(R.id.age);
txtFourth=(TextView) convertView.findViewById(R.id.status);
}else{
}
HashMap<String, String> map=list.get(position);
txtFirst.setText(map.get(FIRST_COLUMN));
txtSecond.setText(map.get(SECOND_COLUMN));
txtThird.setText(map.get(THIRD_COLUMN));
txtFourth.setText(map.get(FOURTH_COLUMN));
return convertView;
}
終於coloumn行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="22sp"/>
<TextView
android:id="@+id/gender"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="22sp"/>
<TextView
android:id="@+id/age"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:textSize="22sp"/>
<TextView
android:id="@+id/status"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:textSize="22sp"/>
的結果是這樣的:
1馬克湯姆森12345678
2 JHON史密斯54654879
3瑪利亞特普45869958
和滾動向上/向下後:
3瑪利亞特普45869958
1馬克湯姆森12345678
2 Jhon Smith 54654879
嘗試改變getItemId:返回位置而不是0 – OShiffer