无法在活动结果()之后更新列表视图;



我确实尝试了" 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");
        }
    }

in InterlistView和适配器内部的代码:

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

肿瘤结果代码,每当选择联系人并负责将数据存储在数据库中:

 @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;
        }
    }

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

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

希望它对您有用。

CustomListAdapter.NotifyDataSetchanged();

相关内容

最新更新