onActivityResult()之后,无法更新列表视图;

时间:2019-03-04 04:57:35

标签: android listview

我确实尝试了CustomAdapter中的notifyDataSetChanged()以及单独线程中的通知,但是无法更新listview。

我有一个应用程序,它可以从联系人列表中获取联系人的姓名和电话号码并存储到sqlite中,并通过读取来获取列表视图中的数据。

我的代码如下:

适配器代码:

public class CustomListAdapter extends ArrayAdapter<CustomDataListModel> {


    ArrayList<CustomDataListModel> customDataListModels;
    Context context;
    int resource;

    public CustomListAdapter(@NonNull Context context, int resource, @NonNull List<CustomDataListModel> objects) {
        super(context, resource,objects);
        this.customDataListModels = customDataListModels;
        this.context = context;
        this.resource = resource;
        notifyDataSetChanged();
    }


    @Nullable
    @Override
    public CustomDataListModel getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if (convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater) getContext()
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.customcontact, null, true);
        }

        final CustomDataListModel customDataListModel=getItem(position);

        TextView Name=(TextView) convertView.findViewById(R.id.nametxtl);
        TextView Number=(TextView) convertView.findViewById(R.id.nutxtl);

        Name.setText(customDataListModel.getName());
        Number.setText(customDataListModel.getNumber());

        return convertView;
    }
}

读取数据并将其添加到customModel的代码:

public void DataRead(){

        Cursor cursor=myhelper.getAllData();
        if(cursor.getCount() ==0){
            ViewDataDialog("Error","Nothing found" );

            return;

        }
        StringBuffer stringBuffer= new StringBuffer();
        while (cursor.moveToNext()) {

            customDataListModelList.add(new CustomDataListModel(cursor.getString(1),cursor.getString(2)));
            stringBuffer.append("Id :"+ cursor.getString(0)+"\n");
            stringBuffer.append("Name :"+ cursor.getString(1)+"\n");
            stringBuffer.append("Number :"+ cursor.getString(2)+"\n");

        }

    }

oncreate内用于初始化listView和适配器的代码:

 DataRead();

customListAdapter=new CustomListAdapter(getApplicationContext(), R.layout.customcontact, customDataListModelList);
listView.setAdapter(customListAdapter);

onActivity结果代码,该代码在选择联系人并负责在数据库中存储数据时调用:

 @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (REQUEST_CODE_ADDRESS_BOOK):
                if (resultCode == Activity.RESULT_OK) {
                    Log.d(TAG, "Response: " + data.toString());
                    uriContact = data.getData();
                    DataAdd(getDisplayName(),getPhNumber());
                    customListAdapter.notifyDataSetChanged();
                    listView.invalidate();
                }
                break;
        }
    }

我不确定这是否正确,但是我尝试使用线程并在onCreate中调用此方法。

 public void threadMethod(){

        ListUpdateThread = new Thread() {
            @Override
            public void run() {
                try {
                    while (!ListUpdateThread.isInterrupted()) {
                        Thread.sleep(500);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                customListAdapter.notifyDataSetInvalidated();
                                customListAdapter.notifyDataSetChanged();
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        ListUpdateThread.start();
    }

1 个答案:

答案 0 :(得分:0)

希望它对您有用。

customListAdapter.notifyDataSetChanged();