Could not set value to the list view using holder - android

when I am trying to set values to the list view using custom adapter it didn't display the data from hash map. values that I want to display in the list view is getting from the database.and my list view contains text views and edit texts.
my code is
private void SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
Log.v("item detailss", c.toString());
while (c.moveToNext()){
HashMap<String, String> datanums = new HashMap<String, String>();
String im_code = c.getString(c.getColumnIndex("im_code"));
String desc = c.getString(c.getColumnIndex("im_desc"));
datanums.put("codec", im_code);
datanums.put("namec", desc);
String price = c.getString(c.getColumnIndex("im_srp"));
datanums.put("imsrpc", price);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
}
private void ViewItems() {
// TODO Auto-generated method stub
list.setAdapter(null);
arrTemp = new String[datas.size()];
datas.add(datanums);
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());
}
private class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
holder.textview3 = (TextView) convertView.findViewById(R.id.code);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
holder.editText2 = (EditText) convertView.findViewById(R.id.pcs);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(datanums.get("namec"));
holder.textView2.setText(datanums.get("imsrpc"));
holder.textview3.setText(datanums.get("codec"));
holder.editText1.setText(arrTemp[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
}
in this viewitems(), and selectitems(), are methods called from oncreate to retrieve and display value in the list view. I couldn't sort out the exact problem.but the edit texts are showing properly in the output.please help.

Add below line at start of the getView method
HashMap<String, String> datanums=datas.get(possition);

public List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
Log.v("item detailss", c.toString());
while (c.moveToNext()){
HashMap<String, String> datanums = new HashMap<String, String>();
String im_code = c.getString(c.getColumnIndex("im_code"));
String desc = c.getString(c.getColumnIndex("im_desc"));
datanums.put("codec", im_code);
datanums.put("namec", desc);
String price = c.getString(c.getColumnIndex("im_srp"));
datanums.put("imsrpc", price);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
/* In your Activity or adapter class call this method(this will return the list) and check the condition if( arraylist!=null && arraylist.size()>0) */

Use this code to set your text view. Hope this works.
holder.textView1.setText(datas.get(position).get("namec"));
holder.textview3.setText(datas.get(position).get("codec"));
holder.textview3.setText(datas.get(position).get("srpc"));

Related

Can not set value to list view using custom adapter

When I am trying to set value to the list view using custom adapter it shows only the last entered value in the hash map. hash map is static.I dont know why I am not getting all the values in the hashmap for that keys I am used in hashmap.
here my code
public class Nextclass extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextclass);
list = (ListView)findViewById(R.id.list1);
SelectItems();
ViewItems();
}
private List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct code ,desc ,name "
+ " from item", null);
while (c.moveToNext()){
String cod = c.getString(c.getColumnIndex("code"));
datanums.put("code", cod);
String desc1 = c.getString(c.getColumnIndex("desc"));
datanums.put("desc", desc1);
String name = c.getString(c.getColumnIndex("name"));
datanums.put("name", name);
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
private void ViewItems() {
// TODO Auto-generated method stub
arrTemp = new String[datas.size()];
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());
}
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.code);
holder.textview3 = (TextView) convertView.findViewById(R.id.desc);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(datanums.get("name"));
holder.textview3.setText(datanums.get("code"));
holder.textView2.setText(datanums.get("desc"));
holder.editText1.setText(arrTemp[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1;
int ref;
}
please help.I used alternate methods,but not worked.thanks in advance
holder.textView1.setText(datas.get(position).get("name"));
holder.textview3.setText(datas.get(position).get("code"));
holder.textView2.setText(datas.get(position).get("desc"));
you are adding hash map in the array list but in the adapter you are trying to get data from the hash map, instead of that you need to take value from arraylist which will return hashmap object and from that object you need to get value for your keys.
hope this will help.

edit text in list view empty or not

In my list view ,I have Edittext and some textviews. when I am trying to iterate over listview to get edittext values, I got some problems.I want to perform actions only if my edittext in the listview is not empty. otherwise i want to continue the iteration.I need only non empty values from the edittexts when iterate through listview. my listview name is "list" here.
my code is
if (list != null) {
for (int i = 0; i < list.getChildCount(); i++) {
ed2 = (EditText) list.getChildAt(i).findViewById(R.id.pcs);
if (ed2.getText().toString().trim().length() != 0) {
TextView im_codetext = (TextView) vie.findViewById(R.id.code);
imcode = im_codetext.getText().toString();
try {
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor cr = newDB.rawQuery("select distinct im_srp_pc, im_subgroup_code from itemmaster where im_code =" + imcode, null);
while (cr.moveToNext()) {
srppc = cr.getString(cr.getColumnIndex("im_srp_pc"));
String subcode = cr.getString(cr.getColumnIndex("im_subgroup_code"));
databas.put("subgp", subcode);
}
srps = Float.valueOf(srpval);
qtys = Integer.valueOf(ed2.getText().toString());
} catch (NumberFormatException e) {
srps = 0;
}
float amnts = srps * qtys;
sumnet = sumnet + amnts;
else{
Log.v(" empty", " empty");
}
}
}
}
My adapter class is
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.srp);
holder.textview3 = (TextView) convertView.findViewById(R.id.code);
holder.editText2 = (EditText) convertView.findViewById(R.id.pcs);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
String [] names = new String[550];
String [] codes = new String[550];
String [] prize = new String[550];
DatabaseHelper dbHelper = new DatabaseHelper(Orders.this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct im_code ,im_desc ,im_srp "
+ " from itemmaster", null);
c.moveToFirst();
while (c.moveToNext()){
Log.v("item detailss", c.toString());
String cod = c.getString(c.getColumnIndex("im_code"));
codes[c.getPosition()] =cod;
String desc1 = c.getString(c.getColumnIndex("im_desc"));
names[c.getPosition()] = desc1;
String price = c.getString(c.getColumnIndex("im_srp"));
prize[c.getPosition()] = price;
}
holder.textView1.setText(names[position+1]);
holder.textview3.setText(codes[position+1]);
holder.textView2.setText(prize[position+1]);
holder.editText2.setText(arrTemp[position]);
holder.editText2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
}
please help.thanks in advance.
I have a Solution.
Declare a HashMap in your custom adapter class.
public class CustomAdapter extends BaseAdapter{
public HashMap<Integer,String> textMap = new HashMap<Integer,String>();
....
In getView() Method get text from HasMap and set in EditText.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.editText = (EditText)rowView.findViewById(R.id.editText);
holder.editText.setText(textMap.get(position));
....
And in TextChangeListner add text in HashMap by position.
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
// arrTemp[holder.ref] = arg0.toString();
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
textMap.put(position,holder.editText.getText().toString());
}
});
Now in your Activity you can get HashMap of adapter and get its value by position. and if there is no text in particuller position ,means edttext is empty.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
listView = (ListView)findViewById(R.id.listView);
final CustomAdapter customAdapter = new CustomAdapter(this,names);
listView.setAdapter(customAdapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<names.length;i++){
Toast.makeText(MainActivity.this, customAdapter.textMap.get(i), Toast.LENGTH_SHORT).show();
}
}
});

add data before sending in android dynamic listview

I have dynamic listview activity it transfer the item to another activity that have edit text(name,phone,table) and another dynamic listview under that.
the each item have edit text it should send with each item.
public class OrderAdapter extends BaseAdapter {
private Context ctx;
private LayoutInflater mInflater;
private List<Item> orderItems;
private DatabaseHelper helper;
public OrderAdapter(Context context, List<Item> items) {
// TODO Auto-generated constructor stub
ctx = context;
helper = new DatabaseHelper(ctx);
mInflater = LayoutInflater.from(ctx);
orderItems = items;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return orderItems.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return orderItems.get(position).getItemID();
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder {
TextView name, desc, price, total;
EditText etQty;
Button btnDelete, btnAdd, btnMinus;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.order_preview_row, parent,
false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.c_table);
holder.desc = (TextView) convertView.findViewById(R.id.order_item_name);
holder.price = (TextView) convertView.findViewById(R.id.tvOpPrice);
holder.etQty = (EditText) convertView.findViewById(R.id.etQty);
holder.btnAdd = (Button) convertView
.findViewById(R.id.btnIncreaseQty);
holder.btnAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
float total_qty = (Float.parseFloat(holder.etQty.getText()
.toString()) + 1);
Item item = (Item) holder.etQty.getTag();
item.setNoItems(String.valueOf(total_qty));
holder.etQty.setText(String.valueOf(total_qty));
holder.total.setText(" = "
+ (Float.parseFloat(orderItems.get(position)
.getItemPrice()) * Float
.parseFloat(holder.etQty.getText().toString())));
}
});
holder.btnMinus = (Button) convertView
.findViewById(R.id.btnDecreaseQty);
holder.btnMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Float total_qty = (Float.parseFloat(holder.etQty.getText()
.toString()) - 1);
if (total_qty != 0) {
Item item = (Item) holder.etQty.getTag();
item.setNoItems(String.valueOf(total_qty));
holder.etQty.setText(String.valueOf(total_qty));
holder.total.setText(" = "
+ (Float.parseFloat(orderItems.get(position)
.getItemPrice()) * Float
.parseFloat(holder.etQty.getText()
.toString())));
}
}
});
holder.btnDelete = (Button) convertView
.findViewById(R.id.btnDeleteItem);
holder.btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
helper.deleteItem(orderItems.get(position).getItemID());
orderItems.remove(position);
notifyDataSetChanged();
}
});
holder.total = (TextView) convertView.findViewById(R.id.tvTotal);
holder.etQty.setTag(orderItems.get(position));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.etQty.setTag(orderItems.get(position));
}
holder.name.setText(orderItems.get(position).getItemName());
holder.desc.setText(orderItems.get(position).getItemDescription());
holder.price.setText(orderItems.get(position).getItemPrice() + " x ");
holder.etQty.setText(orderItems.get(position).getNItems());
holder.total
.setText(" = "
+ (Float.parseFloat(orderItems.get(position)
.getItemPrice()) * Float
.parseFloat(holder.etQty.getText().toString())));
return convertView;
}
}
my problem is how to call the data from edit text to add it with the each item
for example the item name is :"Cheese Pizza" i want it "2 Cheese Pizza"
#Override
protected Boolean doInBackground(Void... params) {
// TODO Auto-generated method stub
item_ids = new StringBuilder();
for (int i = 0; i < orderItems.size(); i++) {
item_ids.append(orderItems.get(i).getItemID()).append(",");
}
if (item_ids.length() == 0)
return false;
else
item_ids.deleteCharAt(item_ids.length() - 1);
Log.d("OrderPreview", item_ids.toString());
item_names = new StringBuilder();
for (int i = 0; i < orderItems.size(); i++) {
item_names.append(orderItems.get(i).getItemName()).append(",");
}
if (item_names.length() == 0)
return false;
else
item_names.deleteCharAt(item_names.length() - 1);
Log.d("OrderPreview", item_names.toString());
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Utils.WS_ADD_ORDER);
List<NameValuePair> mParams = new ArrayList<NameValuePair>();
mParams.add(new BasicNameValuePair("name", etName.getText()
.toString()));
mParams.add(new BasicNameValuePair("phone", etPhone.getText()
.toString()));
mParams.add(new BasicNameValuePair("table", etTable.getText()
.toString()));
mParams.add(new BasicNameValuePair("order_status", "Processing"));
mParams.add(new BasicNameValuePair("id", item_ids.toString()));
mParams.add(new BasicNameValuePair("items", item_names.toString()));
Write a method in your adapter to add new items in existing list of items.
and call that method in your async task when you are getting new dataset from your services/APIs.. Do not forget to put notifyDataSetChanged() in that method inside adapter.

how to show selected data of one custom list to another custom list view on same activity

I have created two textview and one editText when i enter the edit text value and then click on the button to get both textview and edittext value in the second value and which will show in second listview but it will show all data from first list view.
ArrayList<HashMap<String, String>> MyArrList;
String rate;
String itemn;
String quan;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuitem);
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
final ListView lisView2 = (ListView)findViewById(R.id.listView2);
MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
/*** Rows 1 ***/
map = new HashMap<String, String>();
map.put("ID", "Butterscotch");
map.put("Code", "Rs 10");
MyArrList.add(map);
/*** Rows 2 ***/
map = new HashMap<String, String>();
map.put("ID", "Birthday Cake");
map.put("Code", "Rs 100");
MyArrList.add(map);
/*** Rows 3 ***/
map = new HashMap<String, String>();
map.put("ID", "Black Crunch");
map.put("Code", "Rs 102");
MyArrList.add(map);
/*** Rows 4 ***/
map = new HashMap<String, String>();
map.put("ID", "Industrial Chocolate");
map.put("Code", "Rs 200");
MyArrList.add(map);
/*** Rows 5 ***/
map = new HashMap<String, String>();
map.put("ID", "Coffee Molasses Chip");
map.put("Code", " Rs 500");
MyArrList.add(map);
/*** Rows 5 ***/
map = new HashMap<String, String>();
map.put("ID", "Coffee Molasses Chip");
map.put("Code", " Rs 500");
MyArrList.add(map);
lisView1.setAdapter(new CountryAdapter(this));
// Get Item Input
lisView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(Mmnue.this,""+ position ,Toast.LENGTH_LONG).show();
}
});
Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
btnGetItem.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
TextView txtCode = (TextView)findViewById(R.id.ColCode);
TextView itemnm = (TextView)findViewById(R.id.ColID);
EditText txtInput = (EditText)findViewById(R.id.txtInput);
rate = txtCode.getText().toString();
itemnam = txtInput.getText().toString();
quan= itemnm.getText().toString();
int count = lisView1.getAdapter().getCount();
Toast.makeText(Mmnue.this, itemnam + ", " + rate+ " , " + quan ,Toast.LENGTH_LONG).show();
lisView2.setAdapter(new CountryAdapter2(getApplicationContext()));
}
});
}
public class CountryAdapter extends BaseAdapter
{
private Context context;
public CountryAdapter(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
}
public CountryAdapter(OnClickListener onClickListener) {
// TODO Auto-generated constructor stub
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);
}
// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
txtID.setText(MyArrList.get(position).get("ID") +".");
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
txtCode.setText(MyArrList.get(position).get("Code"));
return convertView;
}
}
public class CountryAdapter2 extends BaseAdapter
{
private Context context;
public CountryAdapter2(Context c)
{
//super( c, R.layout.activity_column, R.id.rowTextView, );
// TODO Auto-generated method stub
context = c;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArrList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_mmnue, null);
}
// ColID
TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
//txtID.setText(MyArrList.get(position).get("ID") +".");
txtID.setText(quan);
// ColCode
TextView txtCode = (TextView) convertView.findViewById(R.id.ColCode);
// txtCode.setText(MyArrList.get(position).get("Code"));
txtCode.setText(rate);
return convertView;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
but it will show all data from first list view.
Because you are returning MyArrList.size(); from getCount() of CountryAdapter2. so change it to 1 because you showing only single row in second ListView as:
public int getCount() {
// TODO Auto-generated method stub
return 1;
}

ListView adapter with HashMap

I have a listview with textview and checkbox. The text values are stored in a HashMap. Data is retrieved from a DB. Map key is the ID and Map value is a Location. When I pass the HashMap to the adapter, it starts printing from position 0 (zero). Because of this I get a null text(with no value) and miss one value(the last one) in the HashMap. Can't I use a HashMap as the data source?
EDIT: Here is the code
public class LocationActivity extends Activity{
myAdapter myA;
Map<Integer,String> locLables;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmain);
ListView listview = (ListView) findViewById(R.id.listView1);
String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_LOCATIONLABLES;
SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
locLables = new HashMap<Integer, String>();
if(cursor != null){
if (cursor.moveToFirst()) {
do {
locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
System.out.println(cursor.getString(0)+" "+ cursor.getString(1));
} while (cursor.moveToNext());
}
}
cursor.close();db.close();
//System.out.println(locLables);
myA = new myAdapter(this, locLables, listview);
listview.setAdapter(myA);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(arg2 == 0 )
arg2 = arg2+1;
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();
}
});
}
Here is the Adapter class
class myAdapter extends BaseAdapter{
ListView listView;
Activity cntx;
Map<Integer,String> locations;
List<Integer> locids = new LinkedList<Integer>();
public myAdapter(Activity context,Map<Integer,String> locLables, ListView lv){
cntx = context;
locations = locLables;
listView = lv;
System.out.println(locations);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return locations.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=null;
final int posi = position;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.locationmain_entry, null);
TextView tv = (TextView)row.findViewById(R.id.textView12);
final CheckBox cb = (CheckBox)row.findViewById(R.id.checkBox12);
if(locids.contains(posi))
cb.setChecked(true);
//here I get a null value as the first element and misses the last value..
tv.setText(locations.get(position));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("Chedked" + posi+ locations.get(posi));
//cb.setChecked(true);
if(!locids.contains(posi))
locids.add(posi);
//System.out.println(locids.get(posi));
}else if(!isChecked){
//cb.setChecked(false);
if(locids.contains(posi))
locids.remove(new Integer(posi));
System.out.println("NOt checked" +posi + locations.get(posi));
}
}
});
return row;
}
It runs for the number of elements in the Map. As it is using a 0 (by position variable) it misses the last value as well.
What you can do is
ArrayList<Hashmap<Integer,String>> myList = new ArrayList<Hashmap<Integer,String>>();
if(cursor != null){
if (cursor.moveToFirst()) {
do {
locLables = new HashMap<Integer, String>();
locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
myList.add(locLables);
} while (cursor.moveToNext());
}
}
cursor.close();db.close();
myA = new myAdapter(this, myList, listview);
listview.setAdapter(myA);
You will have to change your "myAdapter" Adapter's constructor to hold "ArrayList> myList" instead of "Map locLables"..
This will help... Let me know if the problem still persists..

Categories

Resources