Custom listView repeating values and changing them on scrolling - android

I have made a listview with a custom adapter. There are five textviews, 1 checkbox and one button.
-------------------------------------
<TextView>
<TextView> <checkbox>
<TextView>
<TextView> <button>
<TextView>
-------------------------------------
The arraylists that I am using to populate the textviews in the list has proper unique data. But the listview takes only four to five values of the begining and repeates it in the rest of the list.
Also when I scroll the values displayed change automatically. For example item A was displayed at first position. So when I scroll down and then come up again, item C or item D is displayed.
It is really confusing me! Please help!
Custom Adapter code
protected class MyCustomAdapter extends ArrayAdapter<String>
{
int len;
private SparseBooleanArray mCheckStates;
private SparseBooleanArray favourites;
ArrayList<String> myMake, myModel, myVer, myPrice, myPlace, sellr_pos;
int count = 0;
ViewHolder holder = null;
Boolean status;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<String> car_make, ArrayList<String> car_model,
ArrayList<String> car_version, ArrayList<String> car_price,
ArrayList<String> car_place, ArrayList<String> sellr_pos) {
super(context, textViewResourceId);
mCheckStates = new SparseBooleanArray(car_model.size());
favourites = new SparseBooleanArray(car_model.size());
myMake = car_make;
myModel = car_model;
myVer = car_version;
myPrice = car_price;
myPlace = car_place;
}
private class ViewHolder {
TextView txt1, txt2, txt3, txt4, txt5;
CheckBox chkbox;
Button btn_fav;
}
#Override
public int getCount() {
return myMake.size();
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.text_adaptr, null);
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById(R.id.text1);
holder.txt2 = (TextView) convertView.findViewById(R.id.text5);
holder.txt3 = (TextView) convertView.findViewById(R.id.text3);
holder.txt4 = (TextView) convertView.findViewById(R.id.text4);
holder.txt5 = (TextView) convertView.findViewById(R.id.text2);
System.out.println("ListView position: " + position);
holder.txt1.setText(myMake.get(position));
holder.txt2.setText(myModel.get(position));
holder.txt3.setText(myVer.get(position));
holder.txt4.setText(myPrice.get(position));
holder.txt5.setText(myPlace.get(position));
holder.chkbox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.btn_fav = (Button) convertView.findViewById(R.id.fav);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkbox.setTag(position);
holder.chkbox.setChecked(mCheckStates.get(position, false));
if (favourites.get(position, false)) {
holder.btn_fav.setBackgroundResource(R.drawable.star);
} else {
holder.btn_fav.setBackgroundResource(R.drawable.star_grey);
}
len = fav_ids.size();
System.out.println("FAV_IDS len:" + len);
for (int t = 0; t < len; t++) {
String pos_id = pos.get(position).trim();
String fav_id = fav_ids.get(t).trim();
if (pos_id.equals(fav_id)) {
holder.btn_fav.setBackgroundResource(R.drawable.star);
break;
} else {
holder.btn_fav.setBackgroundResource(R.drawable.star_grey);
}
}
holder.chkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something
}
});
holder.btn_fav.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something
}
});
return convertView;
}
}

Set Values to the TextView at
if (convertView == null) {
....code...
........../.
/*
you are setting values here
remove it from here
*/
holder.txt1.setText(myMake.get(position));
holder.txt2.setText(myModel.get(position));
holder.txt3.setText(myVer.get(position));
holder.txt4.setText(myPrice.get(position));
holder.txt5.setText(myPlace.get(position));
}
else
{
....code...
}
/*
Set values here
it will solve the problem
*/
holder.txt1.setText(myMake.get(position));
holder.txt2.setText(myModel.get(position));
holder.txt3.setText(myVer.get(position));
holder.txt4.setText(myPrice.get(position));
holder.txt5.setText(myPlace.get(position));
It solves my problem

Related

Duplicate checkbox when checked and row value disappears

I have an application in which there is a listview that displays values and when a row is pressed, the third value of the row will display a value.
Example is: third value is 30 and when it's pressed, it should be
divided by 6, so the answer should be 5.
But when I scroll and press a row in listview example: I pressed row1, there will be a duplicate checked row in row10 and the value of the third row returns to it's old value (30) for example.
Is there any way to keep the checkbox from duplicating and the value of the row preserved when clicked?
Here's my code for the adapter:
public class MyAdapter extends BaseAdapter {
private ArrayList<HashMap<String, String>> mData;
public MyAdapter(ArrayList<HashMap<String, String>> mData2) {
this.mData = mData2;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int i) {
return this.mData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View mView = convertView;
String betid = mData.get(i).get("betid");
ViewHolder holder ;
if (mView == null) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
mView = inflater.inflate(R.layout.row_layout, null,false);
holder = new ViewHolder();
holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
holder.tx_counter = (TextView) mView.findViewById(R.id.tx_counter);
mView.setTag(holder);
} else {
holder = (ViewHolder) mView.getTag();
}
if (betid != null) {
String betnumber = mData.get(i).get("betnumber");
String amountTarget = mData.get(i).get("amountTarget");
String amountRamble = mData.get(i).get("amountRamble");
holder.tx_number.setText(betnumber);
holder.tx_amount.setText(amountTarget);
holder.tx_counter.setText(amountRamble);
}
return mView;
}
private class ViewHolder {
TextView tx_number;
TextView tx_amount;
TextView tx_counter;
}
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkmark);
TextView tv3 = (TextView)view.findViewById(R.id.tx_counter);
EditText editText = (EditText)findViewById(R.id.editText3);
String yy = editText.getText().toString().trim();
String shitts = listView.getItemAtPosition(position).toString();
ArrayList<String> list = new ArrayList<String>();
try {
String[] a = shitts.split(", ");
String[] b = a[1].split("=");
String[] sep = a[0].split("=");
String betnumber = sep[1];
String betamount= b[1];
checkBox.setChecked(!checkBox.isChecked());
if(checkBox.isChecked()){
//sort number
final String sorted = betnumber.chars().sorted().mapToObj(c -> Character.valueOf((char)c).toString()).collect(Collectors.joining());
//check if double digit
Boolean checker = doubleChecker(sorted);
if (checker == true){
Toast.makeText(getApplicationContext(),"DOUBLE DIGIT", LENGTH_SHORT).show();
int answer = Integer.parseInt(betamount) / 3;
tv3.setText(String.valueOf(answer));
}else{
Toast.makeText(getApplicationContext(),"NOT DOUBLE DIGIT", LENGTH_SHORT).show();
int answer;
if(yy.equals("")){
answer = Integer.parseInt(betamount) / 6;
tv3.setText(String.valueOf(answer));
}else{
answer = (Integer.parseInt(betamount) - Integer.parseInt(yy)) / 6;
tv3.setText(String.valueOf(answer));
}
}
//TODO save to array to send
}else{
//TODO mistake RETURN tv3 to old value // remove from array
tv3.setText("0");
}
}catch (Exception e){
}
}
});
I think the issue is here
if (betid != null) {
String betnumber = mData.get(i).get("betnumber");
String amountTarget = mData.get(i).get("amountTarget");
String amountRamble = mData.get(i).get("amountRamble");
holder.tx_number.setText(betnumber);
holder.tx_amount.setText(amountTarget);
holder.tx_counter.setText(amountRamble);
}
You are missing an elsestatement to set other values when betid is null.
If betid is null, other rows can hold values from previous rows.
...
holder.tx_amount.setText(amountTarget);
holder.tx_counter.setText(amountRamble);
} else {
// assuming that if betid is null, then, TextViews should be cleared
// or replace with your own values
holder.tx_number.setText("");
holder.tx_amount.setText("");
holder.tx_counter.setText("");
}

Last value is incremented / decremented while click previous list item in ListView

I am developing an app which is similar to Shopping cart. Here I am using BaseAdapter to populate cart data. There are,
3 ListItems:
Row 1
Row 2
Row 3
When I click increase / decrease button of Row 1 / Row 2, cart item count of the Row 3 is increased / decreased respectively. And I need to calculate TotalSum of the all the product.
Here is my code. Please check it and help me to fix it.
Thanks in advance.
public class CartCountBaseAdapter extends BaseAdapter implements View.OnClickListener {
Context con;
ArrayList<HashMap<String, String>> ArproductMap;
String MYFRAGMENT;
ViewHolder viewHolder;
public CartCountBaseAdapter(Context context, ArrayList<HashMap<String, String>> arproductMap, String myfragment) {
this.con = context;
this.ArproductMap = arproductMap;
this.MYFRAGMENT = myfragment;
}
#Override
public int getCount() {
return ArproductMap.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
viewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) con.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.cart_list_item, null);
}
viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
viewHolder.remove = (Button) convertView.findViewById(R.id.remove);
viewHolder.cartProduct = (TextView) convertView.findViewById(R.id.cartProduct);
viewHolder.cartQuantity = (TextView) convertView.findViewById(R.id.cartQuantity);
viewHolder.cartCount = (TextView) convertView.findViewById(R.id.cartCount);
viewHolder.cartPrice = (TextView) convertView.findViewById(R.id.cartPrice);
viewHolder.cartPriceDum = (TextView) convertView.findViewById(R.id.cartPriceDum);
viewHolder.ivDecrease = (ImageView) convertView.findViewById(R.id.ivDecrease);
viewHolder.ivIncrease = (ImageView) convertView.findViewById(R.id.ivIncrease);
viewHolder.cardView = (CardView) convertView.findViewById(R.id.cardlist_item);
if (MYFRAGMENT == "CheckOutFragment") {
viewHolder.addTowish = (Button) convertView.findViewById(R.id.addTowish);
viewHolder.remove = (Button) convertView.findViewById(R.id.remove);
viewHolder.addTowish.setVisibility(View.GONE);
viewHolder.remove.setVisibility(View.GONE);
viewHolder.cardView.setCardElevation(0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
viewHolder.cardView.setLayoutParams(layoutParams);
}
int CARTPRICE = Integer.parseInt(ArproductMap.get(position).get("Hash_Cart_Price"));
viewHolder.cartProduct.setText(ArproductMap.get(position).get("Hash_Product_Name"));
viewHolder.cartQuantity.setText(ArproductMap.get(position).get("Hash_Cart_Category"));
viewHolder.cartCount.setText(ArproductMap.get(position).get("Hash_Cart_Quantity"));
viewHolder.cartPrice.setText(String.valueOf(CARTPRICE));
viewHolder.cartPriceDum.setText(String.valueOf(CARTPRICE));
viewHolder.Quantity = Integer.parseInt(viewHolder.cartCount.getText().toString());
viewHolder.addTowish.setOnClickListener(this);
viewHolder.remove.setOnClickListener(this);
viewHolder.ivDecrease.setTag(position);
viewHolder.ivIncrease.setTag(position);
viewHolder.ivDecrease.setOnClickListener(this);
viewHolder.ivIncrease.setOnClickListener(this);
return convertView;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivIncrease:
viewHolder.Quantity++;
viewHolder.cartCount.setText(viewHolder.Quantity + "");
break;
case R.id.ivDecrease:
viewHolder.Quantity--;
if (viewHolder.Quantity <= 0) {
viewHolder.Quantity = 0;
viewHolder.cartCount.setText(viewHolder.Quantity + "");
} else {
viewHolder.cartCount.setText(viewHolder.Quantity + "");
}
break;
}
}
static class ViewHolder {
TextView cartProduct,
cartQuantity,
cartCount,
cartPrice,
cartPriceDum;
int Quantity;
ImageView ivDecrease;
ImageView ivIncrease;
Button addTowish;
Button remove;
CardView cardView;
}
}
You should never put any data in your view holder. Put data in any data type like Arraylist. then get the position in your on click and based on that position modify the data
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivIncrease:
int position = (int) v.getTag
datalist[position]++;
viewHolder.cartCount.setText(viewHolder.Quantity + "");
break;
case R.id.ivDecrease:
int position = (int) v.getTag
datalist[position]++;
if (viewHolder.Quantity <= 0) {
viewHolder.Quantity = 0;
viewHolder.cartCount.setText(viewHolder.Quantity + "");
} else {
viewHolder.cartCount.setText(viewHolder.Quantity + "");
}
break;
}
}
Also if you want to make any view related changes put your onclick interface inside viewholder class then use this object

Footer button in listview, how to get value from custom list adapter

I have a listview with switch button in each row. I store sum of checked switches in variable. In MainActivity where I start custom adapter I want to get a sum of checked switches. So how to pass value from custom adapter to parent activity? In the footer of listview I have a button "Dalej" I've create it in MainActivity, and want to get value (this sum) from adapter when user click on it?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cardio);
String[] cardio_1 = getResources().getStringArray(R.array.cardio_1);
ListView listView = (ListView) findViewById(R.id.listView);
View footer = getLayoutInflater().inflate(R.layout.cardio_footer, null);
listView.addFooterView(footer);
ListAdapter listAdapter = new Adapter_cardio(this, cardio_1);
cardio.addAll(Arrays.asList(cardio_1))
listView.setAdapter(listAdapter);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
});
Adapter
public Adapter_cardio(Context context, String[] cardios ) {
super(context, R.layout.cardio_row, cardios);
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("TAG", "tessst1");
zaznaczone = new ArrayList();
}
public String suma(String sum) {
return sum;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
String pytanie = getItem(position);
if (convertView==null){
convertView = inflater.inflate(R.layout.cardio_row, null);
holder = new ViewHolder();
holder.question = (TextView) convertView.findViewById(R.id.question);
holder.s = (Switch)convertView.findViewById(R.id.switch1);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
holder.question.setText(pytanie);
//Model_cardio question = getItem(position);
//final boolean doCheck = (position == 4) || (position == 5);
holder.s.setTag(position);
holder.s.setOnCheckedChangeListener(null);
if (zaznaczone.contains(position) )
{
holder.s.setChecked(true);
}
else
{
holder.s.setChecked(false);
}
holder.s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
zaznaczone.add((Integer) buttonView.getTag());
} else {
zaznaczone.remove((Integer) buttonView.getTag());
}
}
});
sum = zaznaczone.size();
String x = "2";
Log.d("TAG_Switch_all", "suma = " + sum);
return convertView;
}
want to get value (this sum) from adapter when user click on it?
Create a method as public which return sum from Adapter_cardio class as:
public int getSum(){
return zaznaczone.size();
}
and call getSum method on Button click using listAdapter object as:
public void onClick(View v) {
Log.d("TAG_Switch_all", "suma = " + ((Adapter_cardio)listAdapter).getSum());
}
if listAdapter not accessible inside onClick method then declare it as final

Android dynamic Multicolumn Listview

I have a multicolumn List view like the one shown in the image, i used custom adapters to populate this custom list.so the question is how to get data on click of submit button means when i click submit button i should get data like name, price and quantity of only checked checkbox....Thanx in advance.
In my Main xml i have a listview and in mainlist xml i have txtname, txtprice, edittext and checkbox and use efficient adapter.
i'm able to view data in list view, bt the problem is i m unable to save data on click of submit button... so plz help me out, with a sample code, bcz m new to android..
the following is my code..
public class Menu extends Activity {
ListView list;
Cursor cursorMenu;
Button btnPlaceOrder;
Button btnShowOrders;
String Descstr="";
String strtotal="";
List<String[]> lstSelectedItems = null;
DBAdapter db = new DBAdapter(this);
private String[] strName;
private String[] strPrice;
private String[] strDescription;
private class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
try {
return strName.length;
} catch (Exception e) {
//Toast.makeText(Menu.this, "No Data !", Toast.LENGTH_LONG).show();
return 0;
}
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.menulist, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txtItemName);
holder.text2 = (TextView) convertView
.findViewById(R.id.txtPrice);
holder.text3 = (TextView) convertView
.findViewById(R.id.txtDescription);
holder.etext3 = (EditText) convertView
.findViewById(R.id.txtQty);
holder.chk = (CheckBox) convertView
.findViewById(R.id.chkBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(strName[position]);
holder.text2.setText(strPrice[position]);
holder.text3.setText(strDescription[position]);
return convertView;
}
class ViewHolder {
TextView text;
TextView text2;
TextView text3;
EditText etext3;
CheckBox chk;
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
btnPlaceOrder = (Button) findViewById(R.id.btnPlaceOrder);
btnPlaceOrder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
/*db.open();
cursorMenu = db.menu_getAllTitles();
int rowcount = cursorMenu.getCount();
System.out.println("---- +++++ " + rowcount);
System.out.println("---- column +++++ " + cursorMenu.getColumnCount());
int index = 0;
if (rowcount > 0) {
strName = new String[rowcount];
strPrice = new String[rowcount];
strDescription = new String[rowcount];
if (cursorMenu.moveToFirst()) {
do {
strName[index] = cursorMenu.getString(1);
strDescription[index] = cursorMenu.getString(2);
strPrice[index] = cursorMenu.getString(3);
Log.v(TAG, "Name-- " + strName[index] + "Price-- "
+ strPrice[index]);
index++;
} while (cursorMenu.moveToNext());
}
cursorMenu.close();
} else {
Toast.makeText(this, "No Data found", Toast.LENGTH_LONG).show();
}*/
list = (ListView) findViewById(R.id.lstMenu);
list.setAdapter(new EfficientAdapter(this));
System.out.println("--List Child count-----"+list.getChildCount());
System.out.println("--List count-----"+list.getCount());
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"You clciked " + strName[arg2] + "\t" + strPrice[arg2],
Toast.LENGTH_LONG).show();
}
});
}
}
http://www.vogella.de/articles/AndroidListView/article.html go through this example you can get every thing regards listview.

Display ListView TextView based on count acting strange

I have a ListView that is being populated with collection of items using a custom adapter. One of the properties of the collection is a count, and I have two TextViews in my ListView layout, one for the text and one for the count. I'd like not to display the count TextView if the count is zero. The code I have works fine when the ListView is initially loaded, but when I scroll the ListView, the count will show on random rows and constantly change if scroll the ListView up and down. This is the code I have:
public class Main extends ListActivity
{
private static CustomAdapter adapter = null;
#Override
public void onCreate(Bundle icicle)
{
List<Item> items = new ArrayList<Item>();
items = GetItems();
adapter = new CustomAdapter();
for (Item item : items)
adapter.addItem(item);
this.setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
/* ADAPTER */
private class CustomAdapter extends BaseAdapter
{
private final List<Item> mData = new ArrayList<Item>();
private final LayoutInflater mInflater;
public CustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(Item item) {
mData.add(item);
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Object getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
final Item item = (Item)this.getItem(position);
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.main, parent, false);
holder.text = (TextView)convertView.findViewById(R.id.text);
holder.count = (TextView)convertView.findViewById(R.id.count);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText(item.getTitle());
if (item.getCount() > 0)
holder.count.setText(item.getCount().ToString());
else
holder.count.setVisibility(View.INVISIBLE);
return(convertView);
}
}
static class ViewHolder {
TextView text, count
}
Layout:
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
EDIT:
I was able to get it working by doing a hacky workaround. It seems setting the TextView's visibility to Invisible or Gone was causing the issue, so I just changed the color of the items with a zero count to the background color:
if (item.getCount() > 0)
holder.count.setText(item.getCount().ToString());
else
holder.count.setTextColor(Color.WHITE);
If anyone has a real fix, please let me know.
You should just do this:
if (item.getCount() > 0) {
holder.count.setText(item.getCount().ToString());
holder.count.setVisibility(View.VISIBLE);
}
else
holder.count.setVisibility(View.INVISIBLE);
Because you use a ViewHolder, you need to consider the fact that you might get an old View that's not showing the count. This means we need to make sure that the visibility of count is set to VISIBLE. Equally we want to hide it if the count is zero - so we change the visibility even though we might get a View that is all ready invisible.
I think you wrong a part of your code, Amend test this code:
if (item.getCount() > 0)
holder.count.setText(item.getCount());
else
holder.count.setVisibility(View.INVISIBLE);
Please go through this code you may helpful for the same.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
mListUsers = getUsers();
lvUsers = (ListView) findViewById(R.id.lv_user);
s = new ListAdapter(this, R.id.lv_user, mListUsers);
lvUsers.setAdapter(s);
lvUsers.setClickable(true);
// lvUsers.setTextFilterEnabled(true);
lvUsers.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3)
{
Object o = lvUsers.getItemAtPosition(position);
UserBO obj = (UserBO) o;
Intent intent = new Intent(Select.this,Update.class);
intent.putExtra("pid", ""+obj.getId());
intent.putExtra("name", obj.getName());
//put data which you want to show and select.
startActivity(intent);
}
});
}
public ArrayList<UserBO> getUsers()
{
DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
try {
dbAdapter.createDataBase();
} catch (IOException e) {
//Log.i("*** select ",e.getMessage());
}
dbAdapter.openDataBase();
String query="SELECT * FROM profiledatabase";
ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
dbAdapter.close();
ArrayList<UserBO> usersList = new ArrayList<UserBO>();
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> list = stringList.get(i);
UserBO user = new UserBO();
try {
user.pid = Integer.parseInt(list.get(0));
//write code to get data from table
} catch (Exception e) {
//Log.i("***" + Select.class.toString(), e.getMessage());
}
usersList.add(user);
}
return usersList;
}
// ***ListAdapter***
private class ListAdapter extends ArrayAdapter<UserBO> {
// --CloneChangeRequired
private ArrayList<UserBO> mList;
// --CloneChangeRequired
public ListAdapter(Context context, int textViewResourceId,ArrayList<UserBO> list) {
// --CloneChangeRequired
super(context, textViewResourceId, list);
this.mList = list;
}
public View getView(int position, View convertView, ViewGroup parent){
View view = convertView;
try
{
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.list_item, null);
// --CloneChangeRequired(list_item)
}
final UserBO listItem = mList.get(position);
// --CloneChangeRequired
if (listItem != null)
{
// setting list_item views
//( (TextView) view.findViewById(R.id.tv_pid) ).setText( listItem.getId()+"");
( (TextView) view.findViewById(R.id.tv_name) ).setText( listItem.getName() );
( (TextView) view.findViewById(R.id.tv_email)).setText(listItem.getEmail());
( (TextView) view.findViewById(R.id.tv_contact) ).setText( listItem.getContact()+"" );
}}catch(Exception e)
{
//Log.i(Select.ListAdapter.class.toString(), e.getMessage());
}
return view;
}
}

Categories

Resources