1

I am using lazy list adapter for my list view. I trying to get the image url and the text in the list through check box selection but both are always coming irregularly but the position of the list is coming correctly. At the same time when I am pressing first check box it is selecting all check boxes and when I am selecting second box all even check box is getting selected below is my code. Can anybody guide why the data is not properly getting.

      class AllLiteratures extends AsyncTask<String, Void, String> {


    private ProgressDialog dialog = new ProgressDialog(LiteratureList.this);
    @Override
    public void onPreExecute() {
        if(litList == null) {

            // setup your dialog here
            dialog.setMessage("Loading...");
            dialog.setCancelable(false);
            dialog.show();
        }
    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String productConnect = GetStates();
        //set_orienation = getOrient.getOrientation();
        return productConnect;
    }

    private String GetStates() {
        // TODO Auto-generated method stub
           // Hashmap for ListView
        // Creating JSON Parser instance

        if(litList == null) { System.out.println("back");
        JSONParser jParser = new JSONParser(LiteratureList.this);
        try {
            json_literature = jParser.getJsonFromUrl("http://tsubaki.usawebdept.com/?type=listOfLiterature");
            if(json_literature!= null) {
                literature_list = json_literature.toString();
            }
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        }
        return literature_list;
    }
    @SuppressLint("ShowToast")
    public void onPostExecute(String result) {
         System.out.println("result");
         if (result != "" && result != null) {
             dialog.dismiss();

             litList = new  ArrayList<HashMap<String,String>>();
                try {
                    // Getting Array of Contacts
                    us_literature_list = json_literature.getJSONArray("list");
                    lit_url = json_literature.getString("siteUrl")+"/";

                    // looping through All Contacts
                    System.out.println("Length"+us_literature_list.length());
                    for(int i = 0; i < us_literature_list.length(); i++){
                        JSONObject c = us_literature_list.getJSONObject(i);
                        // Storing each json item in variable
                        String name = c.getString(CATEGORY_NAME);
                        String image = c.getString(LARGE_IMAGE.replace(" ", "%20"));
                        String can_order = c.getString(CAN_ORDER);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                        map.put(CATEGORY_NAME, name);
                        map.put(LARGE_IMAGE, lit_url +"/"+ image.replace(" ", "%20"));
                        map.put(CAN_ORDER,can_order);
                                // adding HashList to ArrayList
                        litList.add(map);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                System.out.println("List" +litList);
                lit_home=(ListView) findViewById(R.id.lit_home);
                lit_home.setScrollingCacheEnabled(true);
                lit_home.setFastScrollEnabled(true);
                literature_adapt=new LiteratureAdapter(LiteratureList.this, litList);
                lit_home.setAdapter(literature_adapt);

                OnItemClickListener listener = new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                    }
                };  
                lit_home.setOnItemClickListener(listener);
     } else {
         if(literature_list != null) {
             Toast.makeText(LiteratureList.this, "No Data to Show!", 30).show();
         } else {
             Toast.makeText(LiteratureList.this, "Server is not responding!", 30).show();
         }
     }
    } 

}

//My Lazy Adapter

     class LiteratureAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
LayoutInflater inflater=null;
public ProductImageLoader imageLoader; 
String adapter_for;
int loader1;
ViewHolder viewHolder;
public LiteratureAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    //get_orientation = orientation;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ProductImageLoader(activity.getApplicationContext());
}

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

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

public long getItemId(int position) {
    return position;
}
public class ViewHolder {
   TextView title;
   CheckBox check_lit;
   LinearLayout quantity_selection;
   ImageView thumb_image;
   HashMap<String, String> song;
   String can_order;
   EditText quantity_lit;
   String quantity;
   CheckBox cb;
  // RadioButton cb;
  }
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    viewHolder = new ViewHolder();
    if(convertView==null)
        vi = inflater.inflate(R.layout.literature_items_port, null);
        viewHolder.quantity_lit = (EditText)vi.findViewById(R.id.quantity_lit);
        viewHolder.quantity = viewHolder.quantity_lit.getText().toString(); 
        viewHolder.title = (TextView)vi.findViewById(R.id.catalog_name); // title
        viewHolder.quantity_selection = (LinearLayout)vi.findViewById(R.id.quantity_selection);
        viewHolder.check_lit = (CheckBox)vi.findViewById(R.id.check_lit);
        viewHolder.thumb_image=(ImageView)vi.findViewById(R.id.literature_image); // thumb image

        //ScrollView cat_scroll_list = (ScrollView)vi.findViewById(R.id.cat_scroll_list);
        viewHolder.song = new HashMap<String, String>();
        viewHolder.song = data.get(position);
        //data.add(viewHolder.song);
        viewHolder.title.setText(viewHolder.song.get(LiteratureList.CATEGORY_NAME));
        imageLoader.DisplayImage(viewHolder.song.get(LiteratureList.LARGE_IMAGE),loader1, viewHolder.thumb_image);
        viewHolder.thumb_image.setScaleType(ScaleType.FIT_XY);
        vi.setTag(viewHolder); 

        viewHolder.check_lit
        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                Boolean flag = isChecked;    
                viewHolder.cb = (CheckBox) buttonView; 
                if(viewHolder.cb.isChecked()){      
                    //viewHolder.check_lit.setChecked(viewHolder.song.get(position));///move to here
                    System.out.println("Position"+literature_adapt.getItem(position));
                    System.out.println("Text"+ viewHolder.title.getText());
                    //System.out.println("Image"+ viewHolder.thumb_image.toString());
                    //System.out.println("Order"+ viewHolder.song.get(LiteratureList.CAN_ORDER));
                        //Toast.makeText(context, "Marked as Favorite Game!", 1).show();                  
                }
            }
        }); 
    return vi;
}
}
4

1 回答 1

0

重构和固定适配器,这应该可以解决问题,您可能需要进行一些更改

class LiteratureAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    LayoutInflater inflater=null;
    public ProductImageLoader imageLoader; 
    String adapter_for;
    int loader1;
    int selected = -1; // by default nothing is selected

    public LiteratureAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        //get_orientation = orientation;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ProductImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        if(null == data){ 
            return 0;
        }else{
            return data.size();
        }
    }

    public HashMap<String, String> getItem(int position) {
        if(null == data){
            return null;
        }else{
            return data.get(position);
        }
    }

    public long getItemId(int position) {
        return position;
    }
    public class ViewHolder {
       TextView title;
       CheckBox check_lit;
       LinearLayout quantity_selection;
       ImageView thumb_image;
       HashMap<String, String> song;
       String can_order;
       EditText quantity_lit;
       String quantity;
       CheckBox cb;
    }

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

        ViewHolder viewHolder = null;

        if(convertView==null){
            viewHolder = new ViewHolder();

            convertView = inflater.inflate(R.layout.literature_items_port, null);
            viewHolder.quantity_lit = (EditText)convertView.findViewById(R.id.quantity_lit);

            viewHolder.title = (TextView)convertView.findViewById(R.id.catalog_name); // title
            viewHolder.quantity_selection = (LinearLayout)convertView.findViewById(R.id.quantity_selection);
            viewHolder.check_lit = (CheckBox)convertView.findViewById(R.id.check_lit);
            viewHolder.thumb_image=(ImageView)convertView.findViewById(R.id.literature_image); // thumb image

            //ScrollView cat_scroll_list = (ScrollView)convertView.findViewById(R.id.cat_scroll_list);
            viewHolder.song = new HashMap<String, String>();
            viewHolder.check_lit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {    

                    if(isChecked){                    
                        String sPos = (String)buttonView.getTag();
                        int iPos = Integer.parseInt(pos);
                        if(selected != iPos){
                            selected = iPos;
                            //new Item got selected do your work here
                            Toast.makeText(a.getApplicationContext(), "User selected: "+ iPos, Toast.LENGTH_LONG).show();
                            LiteratureAdapter.this.notifyDataSetChanged();
                        }
                    }
                }
            });
            convertView.setTag(viewHolder); 

        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }   
        //retain the checked state
        if(position == selected){
            viewHolder.check_lit.setChecked(true);
        }else{
            viewHolder.check_lit.setChecked(false); //needs to be reset
        }

        viewHolder.check_lit.setTag(""+position);

        viewHolder.song = getItem(position);

        viewHolder.title.setText(viewHolder.song.get(LiteratureList.CATEGORY_NAME));
        imageLoader.DisplayImage(viewHolder.song.get(LiteratureList.LARGE_IMAGE),loader1, viewHolder.thumb_image);
        viewHolder.thumb_image.setScaleType(ScaleType.FIT_XY);

        return convertView;
    }
}

我基本上保留了最后检查的位置,并在 getView 中检查相同的位置以相应地更新复选框。

于 2013-09-17T11:24:59.277 回答