我正在尝试使用多列格式制作ListView。我找到了一些例子,我已将其更改为测试版。我想创建一个包含蓝牙连接数据的列表(我需要在列表更新时保留我的位置)。
我的活动有一个列表和一个按钮。点击我的列表按钮应显示新值,用数字替换所有首字母引用,但这是发生的事情:
已加载列表:
点击按钮后:
以下是代码:
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
<Button
android:id="@+id/btnOk"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="OK">
</Button>
listview_row.xml
<LinearLayout
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/Text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="First"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/Text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Second"
android:layout_weight="2">
</TextView>
<TextView
android:id="@+id/Text3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Third"
android:layout_weight="1">
</TextView>
<TextView
android:id="@+id/Text4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Fourth"
android:layout_weight="1">
</TextView>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private ArrayList<HashMap<String, String>> list;
private Button btnOk;
private ListView listView;
private ListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put("Text1", "A1");
temp1.put("Text2", "A2");
temp1.put("Text3", "A3");
temp1.put("Text4", "A4");
list.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put("Text1", "B1");
temp2.put("Text2", "B2");
temp2.put("Text3", "B3");
temp2.put("Text4", "B4");
list.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put("Text1", "C1");
temp3.put("Text2", "C2");
temp3.put("Text3", "C3");
temp3.put("Text4", "C4");
list.add(temp3);
adapter = new ListViewAdapter(this, list);
listView.setAdapter(adapter);
btnOk = (Button) findViewById(R.id.btnOk);
// create click listener
View.OnClickListener oclBtnOk = new View.OnClickListener() {
@Override
public void onClick(View v) {
// Limpiar Valor
list.clear();
HashMap<String, String> temp4 = new HashMap<String, String>();
temp4.put("Text1", "11");
temp4.put("Text2", "12");
temp4.put("Text3", "13");
temp4.put("Text4", "14");
list.add(temp4);
HashMap<String, String> temp5 = new HashMap<String, String>();
temp5.put("Text1", "21");
temp5.put("Text2", "22");
temp5.put("Text3", "23");
temp5.put("Text4", "24");
list.add(temp5);
HashMap<String, String> temp6 = new HashMap<String, String>();
temp6.put("Text1", "31");
temp6.put("Text2", "32");
temp6.put("Text3", "33");
temp6.put("Text4", "34");
list.add(temp6);
HashMap<String, String> temp7 = new HashMap<String, String>();
temp7.put("Text1", "41");
temp7.put("Text2", "42");
temp7.put("Text3", "43");
temp7.put("Text4", "44");
list.add(temp7);
HashMap<String, String> temp8 = new HashMap<String, String>();
temp8.put("Text1", "51");
temp8.put("Text2", "52");
temp8.put("Text3", "53");
temp8.put("Text4", "54");
list.add(temp8);
// Actualizar
adapter.notifyDataSetChanged();
}
};
// assign click listener to the OK button (btnOK)
btnOk.setOnClickListener(oclBtnOk);
}
@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);
}
// Adaptador para el ListView
private class ListViewAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txt1;
TextView txt2;
TextView txt3;
TextView txt4;
public ListViewAdapter(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.listview_row, null);
txt1=(TextView) convertView.findViewById(R.id.Text1);
txt2=(TextView) convertView.findViewById(R.id.Text2);
txt3=(TextView) convertView.findViewById(R.id.Text3);
txt4=(TextView) convertView.findViewById(R.id.Text4);
}
HashMap<String, String> map=list.get(position);
txt1.setText(map.get("Text1"));
txt2.setText(map.get("Text2"));
txt3.setText(map.get("Text3"));
txt4.setText(map.get("Text4"));
return convertView;
}
}
答案 0 :(得分:0)
只需创建一个临时对象并像这样重复使用..它可以帮助你......
list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> temp = new HashMap<String, String>();
temp.put("Text1", "A1");
temp.put("Text2", "A2");
temp.put("Text3", "A3");
temp.put("Text4", "A4");
list.add(temp);
temp.clear();
temp.put("Text1", "B1");
temp.put("Text2", "B2");
temp.put("Text3", "B3");
temp.put("Text4", "B4");
list.add(temp);
temp.clear();
temp.put("Text1", "C1");
temp.put("Text2", "C2");
temp.put("Text3", "C3");
temp.put("Text4", "C4");
list.add(temp);