简体   繁体   English

多列ListView不会使用notifyDataSetChanged()刷新

[英]Multicolumn ListView doesn't refresh using notifyDataSetChanged()

I'm trying to make a ListView using a multicolumn format. 我正在尝试使用多列格式制作ListView。 I found some examples and I have changed it for testing. 我找到了一些示例,并对其进行了更改以进行测试。 I want to create list containg data from a Bluetooth connection (And I need to preserve my position when the list is updated). 我想通过蓝牙连接创建包含数据的列表(更新列表时,我需要保留自己的位置)。

My activity have a list and a button. 我的活动有一个列表和一个按钮。 Clicking the button my list should show new values replacing all first letters references with numbers but here is what happens: 单击我的列表按钮应显示新值,用数字替换所有首字母参考,但这是发生的情况:

List loaded: 列表已加载:

在此处输入图片说明

After clicking button: 单击按钮后:

在此处输入图片说明

Here is the code: 这是代码:

activity_main.xml 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 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 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;
    }
}

Just create a single temp object and reuse it like this ..may be it helps you .. 只需创建一个临时对象并像这样重用它即可。可能对您有帮助。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM