通过搜索将项添加到数组

时间:2018-04-22 17:10:48

标签: android arrays listview azure-sql-database

我正在忙于创建一个应用程序,我希望将用户搜索过的所有项目存储在一个数组中,然后显示它。当我运行我的代码时,我收到以下错误:

Process: com.digitialninja.mohammed.curtainclub, PID: 15718
 java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
   at android.widget.SimpleAdapter.getCount(SimpleAdapter.java:100)
   at android.widget.ListView.setAdapter(ListView.java:556)
   at com.digitalninja.mohammed.curtainclub.Srail$1.onClick(Srail.java:60)
   at android.view.View.performClick(View.java:6213)
   at android.widget.TextView.performClick(TextView.java:11074)
   at android.view.View$PerformClick.run(View.java:23645)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6692)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

我正在使用一个SQL查询,它在我的azure数据库中检查记录,然后逻辑上它必须将它添加到一个数组,从那里我的自定义列表视图应该显示所有搜索的项目。这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.srail, container, false);
    search = (Button)rootView.findViewById(R.id.btnSearch);
    Esearch = (EditText)rootView.findViewById(R.id.srch);
    list = (ListView)rootView.findViewById(R.id.raill);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckLogin checkLogin = new CheckLogin();
            checkLogin.execute("");

            List<Map<String,String>> MyData = null;

            String[] fromwhere = { "NAME","PRICE","RANGE","SUPPLIER","SIZE" };

            int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.Range_txtView,R.id.size_txtView,R.id.supplier_txtView};

            ADAhere = new SimpleAdapter(getActivity(), MyData,R.layout.list_products, fromwhere, viewswhere);

            list.setAdapter(ADAhere);


            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    HashMap<String,Object> obj=(HashMap<String,Object>)ADAhere.getItem(position);
                    String ID=(String)obj.get("A");
                    Toast.makeText(getActivity(), ID, Toast.LENGTH_SHORT).show();

                }
            });
        }
    });


    return rootView;
}


public class CheckLogin extends AsyncTask<String, String, String> {
    String z = "";
    Boolean isSuccess = false;

    ProgressDialog progress;

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(getActivity(), "Searching...",
                "Listview Loading! Please Wait...", true);
    }

    @Override
    protected void onPostExecute(String r) {
        progress.dismiss();
        Toast.makeText(getActivity(), r, Toast.LENGTH_SHORT).show();
        if (isSuccess) {
            Toast.makeText(getActivity(), "Search Successfull", Toast.LENGTH_LONG).show();


            //finish();
        }
    }

    @Override
    protected String doInBackground(String... strings) {
        String Search = search.getText().toString();


        if (Search.trim().equals(""))
            z = "Please Search something";
        else {
            List<Map<String, String>> data = null;
            data = new ArrayList<Map<String, String>>();
            try {
                ConnectionHelper conStr = new ConnectionHelper();
                connect = conStr.connectionclass();        // Connect to database
                if (connect == null) {
                    z = "Check Your Internet Access!";
                } else {
                    // Change below query according to your own database.
                    String query = "select * from cc_rail where rail_name='" + Search.toString() + "' OR RAIL_UNIT_PRICE= '" + Search.toString() + "' OR RAIL_RANGE= '" + Search.toString() + "' OR RAIL_SUPPLIER='" + Search.toString() + "' OR RAIL_SIZE='" + Search.toString() + "'";
                    Statement stmt = connect.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    while (rs.next()) {
                        Map<String, String> datanum = new HashMap<String, String>();
                        datanum.put("NAME", rs.getString("RAIL_NAME"));

                        datanum.put("PRICE", rs.getString("RAIL_UNIT_PRICE"));

                        datanum.put("RANGE", rs.getString("RAIL_RANGE"));

                        datanum.put("SUPPLIER", rs.getString("RAIL_SUPPLIER"));

                        datanum.put("SIZE", rs.getString("RAIL_SIZE"));
                        data.add(datanum);
                    }


                    z = " successful";
                    isSuccess = true;
                    connect.close();
                }
            } catch (Exception ex) {
                isSuccess = false;
                z = ex.getMessage();
            }

            return String.valueOf(data);
        }
        return z;
    }

    }

}

2 个答案:

答案 0 :(得分:2)

HTTP

在OnCreateView()方法的这两行中, MyData为null,您将它传递给SimpleAdapter

只需使用适当的数据填充MyData ,您的问题就应该得到解决。

答案 1 :(得分:1)

根据异常的堆栈跟踪,它正在抛出NullPointerException

当您尝试访问NullPointerException对象上的方法或属性时,会抛出null

在审核您的代码后,我认为由于您将MyData作为null传递给方法而引发异常。

我认为初始化MyData对象会修复异常。

这样的事情:
List<Map<String,String>> MyData = new ArrayList();

希望这有帮助。