I want a listview, this Item listview inside each one there are two checkbox. If you just put a listview textview and a checkBOX when you can, but if I change into two will not work. Here is the code:
Disclaimer: This code is modeled http://www.apkbus.com/android-18656-1-1.html inside, personal feel very good, novices can learn together.
MainActivity categories:
package com.example.checkbox;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv = null;
ListView lv = null;
TextView price = null;
String name [] = {"G1", "G2", "G3", "G4", "G5", "G6"};
String priceStr [] = {"10", "20", "25", "30", "40", "50"};
ArrayListlistStr = null;
private List> list = null;
private MyAdapter adapter = null;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
tv = (TextView) this.findViewById (R.id.text);
price = (TextView) this.findViewById (R.id.item_price);
lv = (ListView) this.findViewById (R.id.listview);
showCheckBoxListView ();
}
public void showCheckBoxListView () {
list = new ArrayList> ();
for (int i = 0; iHashMap map = new HashMap ();
map.put ("item_tv", name [i]);
map.put ("item_price", priceStr [i]);
map.put ("item_cb", false);
list.add (map);
adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_cb"}, new int [] {R.id.item_tv, R.id.item_cb});
lv.setAdapter (adapter);
listStr = new ArrayList();
lv.setOnItemClickListener (new OnItemClickListener () {
@ Override
public void onItemClick (AdapterView arg0, View view,
int position, long arg3) {
ViewHolder holder = (ViewHolder) view.getTag ();
holder.cb.toggle () ;/ / every time you get hits, have access to change
checkbox stateMyAdapter.isSelect.put (position, holder.cb.isChecked ()) ;/ / modify the map is worth while to save state
if (holder.cb.isChecked () == true) {
listStr.add (name [position]);
} Else {
listStr.remove (name [position]);
}
tv.setText ("to select" + listStr.size () + "items");
}
});
}
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}
/ ************* MyAdapter class ********************************** /
public static class MyAdapter extends BaseAdapter {
public static HashMapisSelect = null;
private Context context = null;
private LayoutInflater inflater = null;
private List> list = null;
private String keyString [] = null;
private int idValue [] = null;
/ / As each item is displayed in the textview; private String itemName = nullprivate String itemPrice = null;
public MyAdapter (Context context, List> list, int resource, String [] from, int [] to) {
this.context = context;
this.list = list;
keyString = new String [from.length];
idValue = new int [to.length];
System.arraycopy (from, 0, keyString, 0, from.length);
System.arraycopy (to, 0, idValue, 0, to.length);
inflater = LayoutInflater.from (context);
init ();
}
public void init () {
isSelect = new HashMap();
for (int i = 0; iisSelect.put (i, false);
}
}
@ Override
public int getCount () {
return list.size ();
}
@ Override
public Object getItem (int arg0) {
/ / TODO Auto-generated method stub
return list.get (arg0);
}
@ Override
public long getItemId (int arg0) {
/ / TODO Auto-generated method stub
return 0;
}
@ Override
public View getView (int position, View view, ViewGroup arg2) {
ViewHolder holder = null;
if (holder == null) {
holder = new ViewHolder ();
if (view == null) {
view = inflater.inflate (R.layout.list, null);
}
holder.tv = (TextView) view.findViewById (R.id.item_tv);
holder.price = (TextView) view.findViewById (R.id.item_price);
holder.cb = (CheckBox) view.findViewById (R.id.item_cb);
view.setTag (holder);
} Else {
holder = (ViewHolder) view.getTag ();
}
HashMapmap = list.get (position);
if (map! = null) {
itemName = (String) map.get (keyString [0]);
itemPrice = (String) map.get (keyString [1]);
holder.tv.setText (itemName);
holder.price.setText (itemPrice);
}
holder.cb.setChecked (isSelect.get (position));
return view;
}
}
}
ViewHolder class
package com.example.checkbox;
import android.widget.CheckBox;
import android.widget.TextView;
public class ViewHolder {
public TextView tv = null;
public TextView price = null;
public CheckBox cb = null;
}
activity_main code
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
android: orientation = "vertical"
tools: context = ". MainActivity">android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: orientation = "vertical"
>android: id = "@ + id / text"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: text = "@ string / hello_world" />android: id = "@ + id / listview"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
>
list.xml code
android: layout_width = "fill_parent"
android: layout_height = "55dip"
android: orientation = "horizontal"
android: layout_marginTop = "20dip">android: id = "@ + id / item_tv"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: gravity = "center_vertical"
android: layout_marginLeft = "10dip">
android: id = "@ + id / item_price"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
/>android: id = "@ + id / item_cb"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: layout_alignParentRight = "true"
android: layout_alignParentTop = "true"
android: layout_marginRight = "43dp"
android: clickable = "false"
android: focusable = "false"
android: focusableInTouchMode = "false" />
Error message is
09-17 02:42:24.745: E / AndroidRuntime (3032): FATAL EXCEPTION: main
09-17 02:42:24.745: E / AndroidRuntime (3032): java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
09-17 02:42:24.745: E / AndroidRuntime (3032): at com.example.checkbox.MainActivity $ MyAdapter.getView (MainActivity.java: 152)
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
152 lines
itemPrice = (String) map.get (keyString [1]);
Type is boolean
Not a String, not strong converted String
Reply:
Why is a String?
String priceStr [] = {"10", "20", "25", "30", "40", "50"};
map.put ("item_price", priceStr [i]);
This should not be a String type it?
Reply:
keystring in what
Reply:
Constructor Adapter for
public MyAdapter (Context context, List> list, int resource, String [] from, int [] to) {
keyString = new String [from.length];
System.arraycopy (from, 0, keyString, 0, from.length);
This way, then, keyString not a listview inside an array of names of all components installed it?
Reply:
itemPrice = (String) map.get (keyString [1]); the 152 line to itemPrice = "" + map.get (keyString [1]);
Reply:
49 line map.put ("item_cb", false);
52 line adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_cb"}, new int [] {R.id.item_tv, R.id.item_cb});
152 line itemPrice = (String) map.get (keyString [1]);
Please look carefully you this keyString [1] is actually item_cb
May be you neglect it
The effect you want to line 52 should be changed to the following adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_price"}, new int [] {R. id.item_tv, R.id.item_cb});
Reply:
May be you neglect it
The effect you want to line 52 should be changed to the following adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_price"}, new int [] {R. id.item_tv, R.id.item_cb});
Please ignore this answer.
Who is a positive solution upstairs.
Reply:
49 rows map.put ("item_cb", false);
52 line adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_cb"}, new int [] {R.id.item_tv, R.id.item_cb});
152 line itemPrice = (String) map.get (keyString [1]);
Please look carefully you this keyString [1] is actually item_cb
May be you neglect it
The effect you want to line 52 should be changed to the following adapter = new MyAdapter (this, list, R.layout.list, new String [] {"item_tv", "item_price"}, new int [] {R. id.item_tv, R.id.item_cb});
Indeed, I have not really had the item_price into the array, so KeyString [1] is a boolean variable.
Thank you for your answer.
No comments:
Post a Comment