无法解析方法addAll()和Clear()

时间:2019-05-17 04:34:34

标签: android

我已经编写了用于从sqlite获取数据的代码,但是当我使用这两种方法时。.

  1. import React from 'react'; import { View, StyleSheet, WebView } from 'react-native'; import { ExpoLinksView } from '@expo/samples'; export function getBrowerScreen(url) { return class BrowserScreen extends React.Component { static navigationOptions = { header: null, }; render() { return ( <View style={styles.container}> <WebView source={{ uri: url }} style={{ width: '100%' }} /> </View> ); } }; } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', }, }); ,用于使用sqlite在列表中添加数据
  2. buildscript { repositories { jcenter() google() } ,用于清除列表
    两者都不起作用

我通过此链接,但未获得满意的答案。 此错误是什么问题。

这是我的Java文件

addAll()

这是gradle依赖项

clear()

这是我的适配器类

{
ListView listView;
Activity activity;
Button btnAll;
DatabaseHelper databaseHelper;
RecyclerView recyclerViewClients;
ClientRecyclerAdapter clientRecyclerAdapter;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.clients, container, false);

    btnAll=view.findViewById(R.id.searchall);
    clientRecyclerAdapter= new ClientRecyclerAdapter(listView,recyclerViewClients);

    databaseHelper = new DatabaseHelper(activity);
    listView=view.findViewById(R.id.Viewclients);
    getClientFromSqlite();

    btnAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Usman", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

private void getClientFromSqlite() {

    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            listView.clear();
            listView.addAll(databaseHelper.getAllOrderData());
            clientRecyclerAdapter.notifyDataSetChanged();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            clientRecyclerAdapter.notifyDataSetChanged();
        }
    }.execute();
}
}

}

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:design:28.0.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

2 个答案:

答案 0 :(得分:2)

您尚未将适配器连接到listView。将此替换为onCreateView:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.clients, container, false);

    btnAll=view.findViewById(R.id.searchall);
    clientRecyclerAdapter= new ClientRecyclerAdapter(listView,recyclerViewClients);

    databaseHelper = new DatabaseHelper(activity);
    listView=view.findViewById(R.id.Viewclients);

    //missing line
    listView.setAdapter(clientRecyclerAdapter);

    getClientFromSqlite();

    btnAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Usman", Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

ADM强调的另一件事是,您需要在适配器类中编写addAll和clear方法。

用此适配器替换您的适配器:

{

    private List<GetSetOrders> listOrders;
    Context mContext;
    RecyclerView mRecyclerView;
    View itemView;

    public ClientRecyclerAdapter(ListView listOrders, RecyclerView recyclerView) {
        this.listOrders = (List<GetSetOrders>) listOrders;
        mRecyclerView = recyclerView;
    }

    @Override
    public ClientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.items_products_table, parent, false);

        return new ClientViewHolder(itemView);
    }

    public void addAll(ArrayList<GetSetOrders> listOrders) {
        this.listOrders = listOrders;
        notifyDataSetChanged();
    }

    public void clear() {
        this.listOrders.clear();
        notifyDataSetChanged();
    }

    /**
     * ViewHolder class
     */
    public class ClientViewHolder extends RecyclerView.ViewHolder {

        //public AppCompatTextView ID;
        public TextView lblPName;
        public TextView lblTrade;
        public TextView lblRetail;
        public TextView lblSubCategary;
        public TextView lblCompany;
        RelativeLayout layout;

        public ClientViewHolder(View view) {
            super(view);


            lblPName = (TextView) view.findViewById(R.id.lblPName);
            lblTrade = (TextView) view.findViewById(R.id.lblTrade);
            lblRetail = (TextView) view.findViewById(R.id.lblRetail);
            lblSubCategary = (TextView) view.findViewById(R.id.lblSubCategary);
            lblCompany = (TextView) view.findViewById(R.id.lblCompany);
            layout = (RelativeLayout) view.findViewById(R.id.listprod);
        }
    }

    @Override
    public void onBindViewHolder(ClientViewHolder holder, final int position) {

        holder.lblPName.setText(listOrders.get(position).getNAME());
        holder.lblTrade.setText(listOrders.get(position).getORDERCODE());
        holder.lblRetail.setText(listOrders.get(position).getRETAIL());
        holder.lblSubCategary.setText(listOrders.get(position).getSUBCATAGARY());
        holder.lblCompany.setText(listOrders.get(position).getCOMPANY());
        holder.layout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                displayingAlertDialog(position);
                return false;
            }
        });

    }

    private void displayingAlertDialog(int position) {
    }


    @Override
    public int getItemCount() {
        Log.v(ClientRecyclerAdapter.class.getSimpleName(), "" + listOrders.size());
        return listOrders.size();

    }
}

答案 1 :(得分:1)

这是问题。

listView.clear();
listView.addAll(databaseHelper.getAllOrderData());

listView没有任何这些方法。确实ListArrayList具有这些方法。因此,您必须创建List by的实例

  

List listOf = new ArrayList <>();

然后使用

listOf.clear();
listOf.addAll(databaseHelper.getAllOrderData());

DataType将是StringModel ClassInteger等的任何类型。