0

我正在从数据库中读取一些记录并将它们加载到ListView. ListView组成CheckBoxTextView。_ 加载完成AsyncTask。这部分应用程序工作正常。

下一步是根据数据库中的一些标志自动检查一些复选框,这里我遇到了问题。我正在尝试检查里面的那些项目onPostExecute(),然后我得到关于NullPointerException. 例如,如果我从setOnClickListener()按钮小部件中执行相同操作,则它可以正常工作。

问题是如何检查是否ListView已填充,复选框是否已TextView加载并在屏幕上可见?

我不知道它是否会帮助程序中断的代码部分看起来像:

        for (j=0; j<3; j++)
    {
        LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);

        for (k=0; k<rbmjere.size(); k++)
        {
            if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);

            }
        }   
    }

它打破了那条线:

LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);

我必须提到,这仅在我手动按下运行代码以自动检查框的按钮时才有效,并且仅在加载所有数据时才有效。


这是我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spckontrola_update);

    listView = (ListView)findViewById(R.id.listView1);

    btnPohrani = (Button)findViewById(R.id.btnPohrani);

    btnProvjeri = (Button)findViewById(R.id.btnProvjeri);

    btnProvjeri.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            new loadSPCKontrole().execute("FCN");
        }
    });
MyArrList = new ArrayList<HashMap<String, String>>();

public void fillData()
{                       
    SimpleAdapter listadapter = new SimpleAdapter(this, MyArrList, R.layout.activity_list_row,

    new String[] {"OpisMjere", "RbMjere"}, new int[] {R.id.ColOpis, R.id.ColCode});

    listView = (ListView)findViewById(R.id.listView1);
    listView.setAdapter(listadapter);       
}

private class loadSPCKontrole extends AsyncTask<String, Void, Void>
{                   

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SPCUpdate.this);
        pDialog.setMessage("Loading in progress ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(String... params) {

        HashMap<String, String> map;

        String k = params[0].toString();
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("sIDKategorija", k));

        try
        {                       
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.16.48" + "/spc/get_spcmjere.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
        }
        catch(Exception e)
        {
                 Log.e("log_tag", "Error in http connection "+ e.toString());
        }

        try
        {
               BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
               sb = new StringBuilder();
               sb.append(reader.readLine() + "\n");

               String line="0";
               while ((line = reader.readLine()) != null) {
                              sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();

        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        int ct_id;
        String ct_name;

        try
        {

              jArray = new JSONArray(result);
              JSONObject json_data=null;
              for(int i=0;i<jArray.length();i++){
                     json_data = jArray.getJSONObject(i);
                     ct_id=json_data.getInt("RbMjere");
                     ct_name=json_data.getString("OpisMjere");

                     map = new HashMap<String, String>();
                     map.put("RbMjere", String.valueOf(ct_id));
                     map.put("OpisMjere", ct_name);

                     MyArrList.add(map);
                 }                    

         }
        catch(JSONException e1)
        {
            Log.e("Greška konvertiranja", e1.toString());
        } 
        catch (ParseException e1) 
        {
            e1.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Void result)
    {
        super.onPostExecute(result);

        fillData();

        listView.setAdapter(new SPCMjereAdapter(SPCUpdate.this));

        pDialog.dismiss();

    }
}

   public class SPCMjereAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null); 

            }

            // ColID
            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");

            // ColCode
            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));


            // ColChk               
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));

            return convertView;

        }

    }

这是我如何获取应检查的项目的代码:

DB objDB = new DB();
    ArrayList<Integer> rbmjere = objDB.getCheckedSPC(ID);

    int k=0;
    int j=0;
    for (j=0; j<MyArrList.size(); j++)
    {

        LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);

        for (k=0; k<rbmjere.size(); k++)
        {
            if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);
            }
        }   
    }

上面的代码仅在 listview 填充了项目并且此代码位于 OnClickListener() 下时才有效,但如果我从 onPostExecute 运行它则不起作用,因为似乎未加载 listview 中的所有行。所以我的问题是,当所有行的加载完成时,我应该怎么做才能获取信息,然后根据我从数据库中获得的数据检查哪些项目应该被检查?

4

3 回答 3

1

In your case I think that piece of code should be inside your adapter's getView implementation.

Here you go with examples on

And how to use them with your List.

于 2013-07-25T13:00:08.727 回答
1

由于视图回收,listView.getChildAt() 将只返回它正在显示的位置的视图,而不是多个。

你可以在这里检查这个其他问题。它有您正在寻找的答案: ListView getChildAt return null for visible children

于 2013-07-25T12:59:01.603 回答
0

First of all you know when the list is loaded by reading the number of items in your data set and if the listview's data set is equal to that, then all the data is loaded on the listview.. unfortunately I can't help you more with this as I can't see your data load code here..

Second, you should change:

for (j=0; j<3; j++)

with

int listChildSize = listView.getChildCount();
for (j = 0; j < listChildSize ; j++)

EDIT

jfs is right, you should add:

if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);

            }

In a custom Adapter. Here is a sample of custom adapter and also ViewHolder pattern.

于 2013-07-25T13:01:09.503 回答