Java 多列ListView不';t使用notifyDataSetChanged()刷新
我正在尝试使用多列格式创建ListView。我找到了一些示例,并将其更改为测试。我想创建一个包含蓝牙连接数据的列表(当列表更新时,我需要保留我的位置) 我的活动有一个列表和一个按钮。单击按钮“我的列表”应显示新值,将所有首字母引用替换为数字,但发生的情况如下: 已加载列表: 单击按钮后: 代码如下: activity_main.xmlJava 多列ListView不';t使用notifyDataSetChanged()刷新,java,android,android-listview,Java,Android,Android Listview,我正在尝试使用多列格式创建ListView。我找到了一些示例,并将其更改为测试。我想创建一个包含蓝牙连接数据的列表(当列表更新时,我需要保留我的位置) 我的活动有一个列表和一个按钮。单击按钮“我的列表”应显示新值,将所有首字母引用替换为数字,但发生的情况如下: 已加载列表: 单击按钮后: 代码如下: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" and
<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;
}
}
公共类MainActivity扩展了ActionBarActivity{
私有数组列表;
私人按钮btnOk;
私有列表视图列表视图;
私有ListViewAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(listView)findViewById(R.id.listView);
列表=新的ArrayList();
HashMap temp1=新的HashMap();
temp1.put(“Text1”、“A1”);
temp1.put(“Text2”、“A2”);
temp1.put(“Text3”、“A3”);
temp1.put(“Text4”、“A4”);
列表。添加(temp1);
HashMap temp2=新的HashMap();
temp2.put(“Text1”、“B1”);
temp2.put(“Text2”、“B2”);
temp2.put(“Text3”、“B3”);
temp2.put(“Text4”、“B4”);
列表。添加(temp2);
HashMap temp3=新的HashMap();
temp3.put(“Text1”、“C1”);
temp3.put(“Text2”、“C2”);
temp3.put(“Text3”、“C3”);
temp3.put(“Text4”、“C4”);
添加(temp3);
适配器=新的ListViewAdapter(此,列表);
setAdapter(适配器);
btnOk=(按钮)findviewbyd(R.id.btnOk);
//创建单击侦听器
View.OnClickListener oclBtnOk=new View.OnClickListener(){
@凌驾
公共void onClick(视图v){
//利皮亚勇气
list.clear();
HashMap temp4=新的HashMap();
temp4.put(“Text1”、“11”);
temp4.put(“Text2”、“12”);
temp4.put(“Text3”、“13”);
temp4.put(“Text4”、“14”);
添加(temp4);
HashMap temp5=新的HashMap();
temp5.put(“Text1”、“21”);
temp5.put(“Text2”、“22”);
temp5.put(“Text3”、“23”);
temp5.put(“Text4”、“24”);
添加(temp5);
HashMap temp6=新的HashMap();
temp6.put(“Text1”、“31”);
temp6.put(“Text2”、“32”);
temp6.put(“Text3”、“33”);
temp6.put(“Text4”、“34”);
添加(temp6);
HashMap temp7=新的HashMap();
temp7.put(“Text1”、“41”);
temp7.put(“Text2”、“42”);
temp7.put(“Text3”、“43”);
temp7.put(“Text4”、“44”);
列表。添加(temp7);
HashMap temp8=新的HashMap();
temp8.put(“Text1”、“51”);
temp8.put(“Text2”、“52”);
temp8.put(“Text3”、“53”);
temp8.put(“Text4”、“54”);
添加(temp8);
//现实主义者
adapter.notifyDataSetChanged();
}
};
//将click listener分配给OK按钮(btnOK)
btnOk.setOnClickListener(oclBtnOk);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//Adadador para el ListView
私有类ListViewAdapter扩展了BaseAdapter{
公共阵列列表;
活动;
TextView txt1;
TextView txt2;
TextView-txt3;
TextView txt4;
公共ListViewAdapter(活动,ArrayList列表){
超级();
这个。活动=活动;
this.list=list;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回list.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回列表。获取(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
LayoutInflater充气器=活动。getLayoutInflater();
if(convertView==null){
convertView=充气机。充气(R.layout.listview_行,空);
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 map=list.get(位置);
setText(map.get(“Text1”);
setText(map.get(“Text2”);
setText(map.get(“Text3”);
setText(map.get(“Text4”);
返回视图;
}
}
只需创建一个临时对象并像这样重用它。它可能会对您有所帮助
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);
list=newarraylist();
HashMap temp=新的HashMap();
温度输入(“文本1”、“A1”);
温度输入(“文本2”、“A2”);
温度输入(“文本3”、“A3”);
温度输入(“文本4”、“A4”);
列表。添加(临时);
温度清除();
温度输入(“文本1”、“B1”);
温度输入(“文本2”、“B2”);
温度输入(“文本3”、“B3”);
温度输入(“文本4”、“B4”);
列表。添加(临时);
温度清除();
温度输入(“文本1”、“C1”);
温度输入(“文本2”、“C2”);
温度输入(“文本3”、“C3”);
温度输入(“文本4”、“C4”);
列表。添加(临时);