I need to change the image resource according to the message received. I tried to make change in database it will work but to see the change activity want to refresh, on refreshing time connection disconnected. Any solution for this? i can get position of image on receive().. can any one can provide a solution????
public void onDataReceived(String message, String ip) {
/ /--------------------------//
try
{
sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
Cursor allrows = sqlitedatabase.rawQuery("SELECT * FROM "+UserConstruct.image_list.TableName+" WHERE "+UserConstruct.image_list.Device_ID+ "=="+fxddid+"", null);
if (allrows.moveToFirst()) {
do
{
String iconname1 = allrows.getString(1);
String imgidpos1 = allrows.getString(0);
iconnamefxd=iconname1;
imgidpos=Integer.parseInt(imgidpos1);
} while (allrows.moveToNext());
}
allrows.close();
db.close();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "",
Toast.LENGTH_LONG);
}
Toast.makeText(getApplicationContext(), iconnamefxd+"and"+imgidpos, Toast.LENGTH_SHORT).show();
String on="NO";
TextView ststs=(TextView)findViewById(R.id.txtonof);
String a="00";
if(iconnamefxd.equals("0"))
{
sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
String query="UPDATE "+UserConstruct.image_list.TableName+" SET "+UserConstruct.image_list.Image_Name_fxd+"='00' WHERE "+UserConstruct.image_list.Id+" = "+imgidpos+"";
sqlitedatabase.execSQL(query);
call();
Toast.makeText(getBaseContext(), "clear", Toast.LENGTH_LONG).show();
}
else if(iconnamefxd.equals("00"))
{
sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
String query="UPDATE "+UserConstruct.image_list.TableName+" SET "+UserConstruct.image_list.Image_Name_fxd+"='0' WHERE "+UserConstruct.image_list.Id+" = "+imgidpos+"";
sqlitedatabase.execSQL(query);
call();
}
}
else if
{-----------------}
class DrawerListAdapter extends BaseAdapter
{
Context mContext;
ArrayList<NavItem> mNavItems;
public DrawerListAdapter(Context context, ArrayList<NavItem> navItems)
{
mContext = context;
mNavItems = navItems;
}
#Override
public int getCount() {
return mNavItems.size();
}
#Override
public Object getItem(int position) {
return mNavItems.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.itemgridlayout, null);
}
else {
view = convertView;
}
TextView letterText = (TextView) view.findViewById(R.id.item_title);
TextView dvcid=(TextView)view.findViewById(R.id.txtid);
ImageView iconView = (ImageView) view.findViewById(R.id.item_img);
TextView sttus=(TextView)findViewById(R.id.txtonof);
//ImageView iconView = (ImageView) view.findViewById(R.id.icon);
dvcid.setText(mNavItems.get(position).DvceId);
letterText.setText(mNavItems.get(position).IconName);
String fx=mNavItems.get(position).Icon;
if(fx.equals("0"))
{iconView.setImageResource(R.drawable.off1);}
else if(fx.equals("1"))
{--------------}
public void call()
{
try {
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
getTableValues(mNavItems);
try {
final DrawerListAdapter im = new DrawerListAdapter(this, mNavItems);
im.notifyDataSetChanged();
gridview.invalidateViews();
}catch (NullPointerException e){e.printStackTrace();}
You can pass the message params to the Adapter and and set the Resource based on that. Call notifyDataSetChanged after changing the resource.
Do below code
NavItem navItem=(NavItem)adapter.getItem(position);
navIte,.setImageId(newId);
adapter.notifyDatasetChanged();
In every row of my ListView I have a checkbox with a listener. The Listener update databse row.
MyAdapter class is :
public class MyAdapter extends CursorAdapter {
Context b;
LayoutInflater inflater;
#SuppressWarnings("deprecation")
public MyAdapter(Context context, Cursor c) {
super(context, c);
inflater = LayoutInflater.from(context);
b= (Context) context;
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);
tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));
final int pos = cursor.getPosition();
final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);
String likes = cursor.getString(cursor.getColumnIndex("like"));
if (likes.equals("yes")) {
repeatChkBx.setChecked(true);
} else {
repeatChkBx.setChecked(false);
}
repeatChkBx.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MyDatabase mydatabase = new MyDatabase(b);
SQLiteDatabase mydb = mydatabase.getWritableDatabase();
cursor.moveToPosition(pos);
if (repeatChkBx.isChecked()) {
ContentValues cv = new ContentValues();
cv.put("like", "yes");
mydb.update("list", cv, "id ="+cursor.getString(1), null);
} else {
ContentValues cv = new ContentValues();
cv.put("like", "no");
mydb.update("list", cv, "id ="+cursor.getString(1), null);
}
mydb.close();
}
});
}
protected Context getActivity() {
// TODO Auto-generated method stub
return null;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.item, parent, false);
}
}
I have this problem that moving the list I lost the checkbox state for row that disappear from screen.
like this pic :
check below code, I have made some changes in your code,... hope it will solve your problem
public class MyAdapter extends CursorAdapter {
Context b;
LayoutInflater inflater;
ArrayLis<Boolean> checkbox_arr = new ArrayLis<Boolean>();
#SuppressWarnings("deprecation")
public MyAdapter(Context context, Cursor c) {
super(context, c);
for(int i=0;i<c.getCount();i++){
checkbox_arr.add(false);
}
inflater = LayoutInflater.from(context);
b= (Context) context;
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
TextView tv1 = (TextView)view.findViewById(R.id.txt_name);
TextView tv2 = (TextView)view.findViewById(R.id.txt_numer);
tv1.setText(cursor.getString(2));
tv2.setText(cursor.getString(3));
final int pos = cursor.getPosition();
final CheckBox repeatChkBx = (CheckBox)view.findViewById(R.id.favorite_check);
String likes = cursor.getString(cursor.getColumnIndex("like"));
if (likes.equals("yes")) {
checkbox_arr.set(pos,true);
} else {
checkbox_arr.set(pos,false);
}
repeatChkBx.setChecked(checkbox_arr.get(pos));
repeatChkBx.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
MyDatabase mydatabase = new MyDatabase(b);
SQLiteDatabase mydb = mydatabase.getWritableDatabase();
cursor.moveToPosition(pos);
if (repeatChkBx.isChecked()) {
checkbox_arr.set(pos,false); //
ContentValues cv = new ContentValues();
cv.put("like", "no");
mydb.update("list", cv, "id ="+cursor.getString(1), null);
} else {
checkbox_arr.set(pos,true); //
ContentValues cv = new ContentValues();
cv.put("like", "yes"); //
mydb.update("list", cv, "id ="+cursor.getString(1), null);
}
repeatChkBx.setChecked(checkbox_arr.get(pos)); //
notifyDataSetChanged();
mydb.close();
}
});
}
protected Context getActivity() {
// TODO Auto-generated method stub
return null;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.item, parent, false);
}
}
setting the text view according its position in of the row a list view filled from SimpleCursorAdapter from database on the click of the onlistitemclicked
my custom list view has an image& textview for a name and another invisible text view which will be visible and be setting with a different number on the row clicked only but the problem is that when i clicked any row the text appeared on the view at the first row only whatever the row i clicked
and I tried to use the set and get methods but i found its used for the Base Adapter.
and the textview which will visible is not from data base
can some one tell me how to do it please
here is a part of the code
public class Select_players_two extends ListActivity
{
protected static class RowViewHolder
{
public TextView tvOne;
public TextView tvTwo;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
public class CustConAdpterSelect extends SimpleCursorAdapter
{
int i = 0;
int count;
private int layout;
LayoutInflater inflator;
final SQLiteConnector sqlCon = new SQLiteConnector(mContext);
private ImageButton editBtn;
private ImageButton delBtn;
int id ;
TextView txt_select;
CharSequence txt_char;
static final String KEY_No = "playerNo";
public CustConAdpterSelect(Context context, int layout, Cursor c,
String[] from, int[] to, int flags)
{
super(context, layout, c, from, to,0);
this.layout = layout;
inflator= LayoutInflater.from(context);
}
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.lv_name_photo, parent, false);
RowViewHolder holder = new RowViewHolder();
holder.tvOne = (TextView) retView.findViewById(R.id.name);
holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
// holder.tvOne.setOnClickListener(tvOneLapOnClickListener);
retView.setTag(holder);
return retView;
}
#Override
public void bindView(View v, final Context context, Cursor c)
{
editBtn=(ImageButton) v.findViewById(R.id.edit_btn);
if( editBtn.getVisibility() == View.VISIBLE )
editBtn.setVisibility(View.INVISIBLE);
else
editBtn.setVisibility(View.INVISIBLE);
//set delete button invisble
delBtn=(ImageButton) v.findViewById(R.id.del_btn);
if( delBtn.getVisibility() == View.VISIBLE )
delBtn.setVisibility(View.INVISIBLE);
else
delBtn.setVisibility(View.INVISIBLE);
//final int
id = c.getInt(c.getColumnIndex(Contacts.ID));
final String name = c.getString(c.getColumnIndex(Contacts.NAME));
final String phone = c.getString(c.getColumnIndex(Contacts.PHONE));
final String email = c.getString(c.getColumnIndex(Contacts.MAIL));
final String fb = c.getString(c.getColumnIndex(Contacts.FB));
final byte[] image = c.getBlob(c.getColumnIndex(Contacts.IMAGE));
ImageView iv = (ImageView) v.findViewById(R.id.photo);
if (image != null)
{
if (image.length > 3)
{
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
}
}
TextView tname = (TextView) v.findViewById(R.id.name);
tname.setText(name);
TextView tphone = (TextView) v.findViewById(R.id.phone);
tphone.setText(phone);
TextView temail = (TextView) v.findViewById(R.id.email);
temail.setText(email);
txt_select=(TextView)v.findViewById(R.id.txt_number);
// final SQLiteConnector sqlCon = new SQLiteConnector(context);
//for( i = 0; i <=4; i++)
/*
v.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//onitem clicked
txt_char = txt_select.getText();
{
if (txt_char != null)
{
int txt_int = Integer.parseInt(txt_char.toString());
int count = txt_int;
Log.d("count1",String.valueOf(count));
count++;
txt_select.setText(String.valueOf(count));
Log.d("count",String.valueOf(count));
if( txt_select.getVisibility() == View.INVISIBLE )
txt_select.setVisibility(View.VISIBLE);
else
txt_select.setVisibility(View.INVISIBLE);
Log.d("number", String.valueOf(i));
}
/* if( txt_select.getVisibility() == View.INVISIBLE )
txt_select.setVisibility(View.VISIBLE);
else
txt_select.setVisibility(View.INVISIBLE);
i++;
Log.d("number", String.valueOf(i));*/
/* }
}
});
*/
}
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
final View view = super.getView(position, convertView, parent);
final TextView textView = (TextView)view.findViewById(R.id.txt_number);
/* textView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Log.i("Click", "TextView clicked on row " + position);
// textView.setTag(position);
txt_char = txt_select.getText();
Log.d("txt", txt_char.toString());
if (txt_char != null)
{
int txt_int = Integer.parseInt(txt_char.toString());
Log.d("txt2", txt_char.toString());
int count = txt_int;
Log.d("count1",String.valueOf(count));
count++;
txt_select.setText(String.valueOf(count));
CharSequence txt_char2 = txt_select.getText();
Log.d("ttxt", txt_char2.toString());
Log.d("count",String.valueOf(count));
if( txt_select.getVisibility() == View.INVISIBLE )
txt_select.setVisibility(View.VISIBLE);
else
txt_select.setVisibility(View.INVISIBLE);
Log.d("number", String.valueOf(i));
i++;
}
}*/
// }); // TODO Auto-generated method stub
return view;
}
private OnClickListener tvOneLapOnClickListener = new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// get the RowViewHolder
RowViewHolder holder = new RowViewHolder();
// Get the holder for the row
holder = (RowViewHolder) ((View) v.getParent()).getTag();
if (holder.tvOne.getVisibility() == View.INVISIBLE)
holder.tvOne.setVisibility(View.VISIBLE);
else
holder.tvOne.setVisibility(View.INVISIBLE);
}
};
}
}
that is the database class "SQLiteConnector"
public class SQLiteConnector
{
private SQLiteDatabase db;
private SQLiteHelper sqlHp,sqlhpc;
private Cursor cur,curc;
public SQLiteConnector(Context context)
{
sqlHp = new SQLiteHelper(context, Contacts.DB_NAME, null, 1);
sqlhpc = new SQLiteHelper(context, Contacts.DB_NAME, null, 1);
}
// insert new player in the list//
public void insertContact(String name, String phone, String mail,String fb,byte[] blob) {
ContentValues cv = new ContentValues();
cv.put(Contacts.NAME, name);
cv.put(Contacts.PHONE, phone);
cv.put(Contacts.MAIL, mail);
cv.put(Contacts.FB, fb);
cv.put(Contacts.IMAGE,blob);
db = sqlHp.getWritableDatabase();
db.insert(Contacts.TABLE, null, cv);
db.close();
}
// insert the score sheet //
public void insertContact_score(String score, String num_call, String num_collection ,String shape_type,
String score_sec, String num_call_sec, String num_collection_sec ,String shape_type_sec,
String score_third, String num_call_third, String num_collection_third ,String shape_type_third,
String score_forth, String num_call_forth, String num_collection_forth ,String shape_type_forth)
{
ContentValues cvscore = new ContentValues();
cvscore.put(Contacts.SCORE_st, score);
cvscore.put(Contacts.NUM_CALL_st, num_call);
cvscore.put(Contacts.NUM_COLLECTION_st, num_collection);
cvscore.put(Contacts.SHAPE_CALL_st, shape_type);
cvscore.put(Contacts.SCORE_sec, score_sec);
cvscore.put(Contacts.NUM_CALL_sec, num_call_sec);
cvscore.put(Contacts.NUM_COLLECTION_sec, num_collection_sec);
cvscore.put(Contacts.SHAPE_CALL_sec, shape_type_sec);
cvscore.put(Contacts.SCORE_third, score_third);
cvscore.put(Contacts.NUM_CALL_third, num_call_third);
cvscore.put(Contacts.NUM_COLLECTION_third, num_collection_third);
cvscore.put(Contacts.SHAPE_CALL_third,shape_type_third);
cvscore.put(Contacts.SCORE_forth, score_forth);
cvscore.put(Contacts.NUM_CALL_forth, num_call_forth);
cvscore.put(Contacts.NUM_COLLECTION_forth, num_collection_forth);
cvscore.put(Contacts.SHAPE_CALL_forth,shape_type_forth);
//cvscore.put(Contacts.IMAGE,blob);
db = sqlhpc.getWritableDatabase();
db.insert(Contacts.TABLESCORE, null, cvscore);
db.close();
}
public void updateContact_score(long id,String score, String num_call, String num_collection ,String shape_type,
String score_sec, String num_call_sec, String num_collection_sec ,String shape_type_sec,
String score_third, String num_call_third, String num_collection_third ,String shape_type_third,
String score_forth, String num_call_forth, String num_collection_forth ,String shape_type_forth)
{
ContentValues cvscore = new ContentValues();
cvscore.put(Contacts.SCORE_st, score);
cvscore.put(Contacts.NUM_CALL_st, num_call);
cvscore.put(Contacts.NUM_COLLECTION_st, num_collection);
cvscore.put(Contacts.SHAPE_CALL_st, shape_type);
cvscore.put(Contacts.SCORE_sec, score_sec);
cvscore.put(Contacts.NUM_CALL_sec, num_call_sec);
cvscore.put(Contacts.NUM_COLLECTION_sec, num_collection_sec);
cvscore.put(Contacts.SHAPE_CALL_sec, shape_type_sec);
cvscore.put(Contacts.SCORE_third, score_third);
cvscore.put(Contacts.NUM_CALL_third, num_call_third);
cvscore.put(Contacts.NUM_COLLECTION_third, num_collection_third);
cvscore.put(Contacts.SHAPE_CALL_third,shape_type_third);
cvscore.put(Contacts.SCORE_forth, score_forth);
cvscore.put(Contacts.NUM_CALL_forth, num_call_forth);
cvscore.put(Contacts.NUM_COLLECTION_forth, num_collection_forth);
cvscore.put(Contacts.SHAPE_CALL_forth,shape_type_forth);
//cvscore.put(Contacts.IMAGE,blob);
db = sqlhpc.getWritableDatabase();
db.update(Contacts.TABLESCORE, cvscore, Contacts.ID+"="+ id, null);
db.close();
}
public void updateContact(long id,String name, String phone, String mail,String fb,byte[] blob) {
ContentValues cv = new ContentValues();
cv.put(Contacts.NAME, name);
cv.put(Contacts.PHONE, phone);
cv.put(Contacts.MAIL, mail);
cv.put(Contacts.FB, fb);
cv.put(Contacts.IMAGE,blob);
db = sqlHp.getWritableDatabase();
db.update(Contacts.TABLE, cv, Contacts.ID+"="+ id, null);
db.close();
}
public Cursor getAllContacts() {
db = sqlHp.getReadableDatabase();
cur=db.query(Contacts.TABLE,null, null,null, null, null, "name");
return cur;
}
public Cursor getAllScores() {
db = sqlhpc.getReadableDatabase();
curc=db.query(Contacts.TABLESCORE,null, null,null, null, null, "score_st");
return curc;
}
public void deletescore(long id) {
System.out.println("DELETE ");
db = sqlhpc.getWritableDatabase();
db.delete(Contacts.TABLESCORE, "_id="+id , null);
db.close();
}
public Cursor getOneContact(long id) {
db = sqlHp.getReadableDatabase();
cur=db.query(Contacts.TABLE, null, "_id="+ id, null, null, null,null);
return cur;
}
public void deleteContact(long id) {
System.out.println("DELETE ");
db = sqlHp.getWritableDatabase();
db.delete(Contacts.TABLE, "_id="+id , null);
db.close();
}
}
I believe what you need to do is use the Tag facility of the row view container to identify the textview it contains and then retrieve it in the listener to identify the correct textview. The way I've done it is to use a holder for the row contained views, so you can access any that you need. For example:
protected static class RowViewHolder {
public TextView tvOne;
public TextView tvTwo;
}
Then in your newView method, populate the holder and set the Tag to it:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.single_row_item, parent, false);
RowViewHolder holder = new RowViewHolder();
holder.tvOne = (TextView) retView.findViewById(R.id.name);
holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
retView.setTag(holder);
return retView;
}
In your listener then you can access the correct textview:
public void onClick(View v) {
ListView lv = (ListView) v.getParent();
final int position = lv.getPositionForView((View) v.getParent());
// get the RowViewHolder
RowViewHolder holder = new RowViewHolder();
holder = (RowViewHolder) ((View) v.getParent()).getTag();
if(holder.tvOne.getVisibility() == View.INVISIBLE ) {
holder.tvOne.setVisibility(View.VISIBLE);
}
else
{
holder.tvOne.setVisibility(View.INVISIBLE);
}
}
Apologies, but I've not been able to test this code but I hope it gives you a pointer to the process. You shouldn't need the RowViewHolder but I've included it so you or others can see how to access multiple views within the row.
You may find the this video helpful The World of ListView I think you'll find the relevant discussion at about 4:10 in which describes how the views are res-used and how index, position etc. relate.
I also found one of the answers to this question helpful Android: ListView elements with multiple clickable buttons
There are multiple ways of doing this. The way I was suggesting assumes you have a single code module for the activity containing the CustConAdpterSelect class. So the structure looks something like this.
package com.example.totastest;
// imports
public class Copy_2_of_MainActivity extends Activity {
protected static class RowViewHolder
{
public TextView tvOne;
public TextView tvTwo;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public class CustConAdpterSelect extends SimpleCursorAdapter
{
// ...
public CustConAdpterSelect(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to, 0);
// ...
}
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View retView = inflater.inflate(R.layout.lv_name_photo, parent, false);
RowViewHolder holder = new RowViewHolder();
holder.tvOne = (TextView) retView.findViewById(R.id.name);
holder.tvTwo = (TextView) retView.findViewById(R.id.txt_number);
holder.tvOne.setOnClickListener(tvOneLapOnClickListener);
retView.setTag(holder);
return retView;
}
#Override
public void bindView(View v, final Context context, Cursor c)
{
// ...
}
#SuppressWarnings("unchecked")
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
// ...
}
private OnClickListener tvOneLapOnClickListener = new OnClickListener() {
#Override
// When the tvOne button is clicked, execute this code
public void onClick(View v) {
// get the RowViewHolder
RowViewHolder holder = new RowViewHolder();
// Get the holder for the row
holder = (RowViewHolder) ((View) v.getParent()).getTag();
if (holder.tvOne.getVisibility() == View.INVISIBLE)
holder.tvOne.setVisibility(View.VISIBLE);
else
holder.tvOne.setVisibility(View.INVISIBLE);
}
};
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
// ...
}
}
How does my listview automatically display data alphabetically. Because in my emulator it display data arrange by how it arranged in my database.
can anyone have a explanation with this issue?
this is how i retrieve data from my database.
SQLiteDatabase db = openOrCreateDatabase("database", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT DISTINCT category,cimage from FoodList", null);
int count = c.getCount();
String[] values = new String[count];
String[] values1 = new String[count];
c.moveToFirst();
for(int x = 0; x < count; x++){
values[x] = c.getString(c.getColumnIndex("category"));
values1[x] = c.getString(c.getColumnIndex("cimage"));
c.moveToNext();
}
list.setAdapter(new imageadapter(this,values,values1));
db.close();
c.close();
here is my imageadapter activity
public class imageadapter extends BaseAdapter {
private Context context;
private final String[] mobileValues;
public final String[] mobileValues2;
public imageadapter(Context context, String[] mobileValues, String[] mobileValues1) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileValues2 = mobileValues1;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewHolder holder = new viewHolder();
if (convertView == null) {
convertView = inflater.inflate(R.layout.logo, null);
holder.category = (TextView) convertView.findViewById(R.id.cat);
holder.image = (ImageView) convertView.findViewById(R.id.imglogo);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.category.setText(mobileValues[position]);
holder.s = mobileValues2[position];
byte[] decodedString = null;
try {
decodedString = Base64.decode(holder.s, 0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
holder.image.setImageBitmap(decodedByte);
return convertView;
}
public int getCount() {
return mobileValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
static class viewHolder{
ImageView image;
TextView category;
String s;
}
}
get the data from the database by arranging it in the order you are intersted and next add to the arrayadapter.
This may solve your problem
Are you using any query for getting the information from the database? If so, why don't you use a SQL statement like this:
SELECT * FROM your_table ORDER BY your_column
public Cursor fetchAllData() {
return database.query(urtablename, null, null, null, null, null,
null);
}
it will return all data using cursor,In ur activity
Cursor cr=dba.fetchAllData();
cr.movetofirst();
while(!cr.isAfterLast())
{
//fetch all data using column name
//like cr.getString(cr.getColumnIndex("ur column name"));
//it will return as per in dATABASE
cr.movetoNext();
}
Create a custom comparator as you need. And now Use this in the sort function of ArrayAdapter.
try this in constructor :
public imageadapter(Context context, String[] mobileValues, String[] mobileValues1) {
this.context = context;
this.mobileValues = mobileValues;
this.mobileValues2 = mobileValues1;
Arrays.sort(mobileValues);
//Arrays.sort(mobileValues2);
}
enjoy.
I am getting the data from sqlite database and trying to display it in CustomeListView But it is displaying some items example i can see six items in my listView at a time then it is showing six items when i scroll it, it will repeatedly show same items HERE IS MY CODE
Here i am querying from database
public ArrayList<GetEmailFromDatabase> emails_From_Database() {
SQLiteDatabase db = helper.getReadableDatabase();
try {
GetEmailFromDatabase gefd;
ArrayList<GetEmailFromDatabase> results = new ArrayList<GetEmailFromDatabase>();
Cursor c = db.rawQuery("select * from Emailreceived", null);
if (c.getCount() > 0) {
int i=0;
c.moveToPosition(i);
do {
gefd = new GetEmailFromDatabase();
gefd.setMessage_No(c.getInt(c.getColumnIndex("_Id")));
gefd.setFrom(c.getString(c.getColumnIndex("EmailFrom")));
gefd.setDate(c.getString(c.getColumnIndex("EmailDate")));
gefd.setSubject(c.getString(c.getColumnIndex("Subject")));
results.add(gefd);
gefd = null;
i++;
} while (c.moveToPosition(i));
c.close();
}
return results;
} finally {
if (db != null)
db.close();
}
}
This is my listActivity with custom list
public class Email_listActivity extends MenuOptions{
//Database getEmails = new Database(getApplicationContext());
String items[]={"red","white","Black","green","brown","yellow","blue","pink","orange"};
Database database;
String mData;
ArrayList<GetEmailFromDatabase> emailResults;
Database db1;
private EmailListFeedAdapter emailListFeedAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.email_list_main);
emailResults = new ArrayList<GetEmailFromDatabase>();
emailListFeedAdapter = new EmailListFeedAdapter(this, R.layout.email_listview_row, emailResults);
setListAdapter(this.emailListFeedAdapter);
getResults();
if(emailResults != null && emailResults.size() > -1){
emailListFeedAdapter.notifyDataSetChanged();
for(int i=0;i< emailResults.size();i++){
emailListFeedAdapter.add( emailResults.get(i));
Log.e("%%%%%%%%%%%%%%%%%%%", "emailResults -------->"+emailListFeedAdapter.areAllItemsEnabled());
}
}
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
int pos = position;
String s = (String) ((TextView) v.findViewById(R.id.item_number)).getText();
database = new Database(getApplicationContext());
mData = database.getDate(s);
Intent i = new Intent(Email_listActivity.this, EmailWebView.class);
i.putExtra("webData", mData);
startActivity(i);
Log.e("$$$$$$$$$$$$", "POSITION--------->"+position+" View-------->"+v);
Toast.makeText(getApplicationContext(), "Hello"+v, Toast.LENGTH_SHORT).show();
}
public void getResults(){
db1 = new Database(getBaseContext());
emailResults = new ArrayList<GetEmailFromDatabase>();
//ArrayList<GetEmailFromDatabase> fromdatabase = db1.emails_From_Database();
emailResults = db1.emails_From_Database();
}
public class EmailListFeedAdapter extends ArrayAdapter<GetEmailFromDatabase>{
private ArrayList<GetEmailFromDatabase> itemss;
public EmailListFeedAdapter(Context context, int textViewResourceId, ArrayList<GetEmailFromDatabase> items) {
super(context, textViewResourceId, items);
this.itemss = items;
}
#Override
public View getView(int position, View emailView, ViewGroup parent) {
if(emailView == null){
LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
emailView= inflater.inflate(R.layout.email_listview_row, null);
GetEmailFromDatabase o = itemss.get(position);
if(o!=null){
TextView messageNo = (TextView)emailView.findViewById(R.id.item_number);
TextView subject= (TextView)emailView.findViewById(R.id.emaillistrow_txt_partofemail);
TextView sender= (TextView)emailView.findViewById(R.id.emaillistrow_txt_sender);
TextView datetime= (TextView)emailView.findViewById(R.id.emaillistrow_txt_datetime);
ImageView icon= (ImageView)emailView.findViewById(R.id.emaillistrow_icon_indicator);
icon.setImageResource(R.drawable.y_icon);
if(messageNo!=null){
messageNo.setText(Integer.toString(o.getMessage_No()));
}
if(subject!=null){
subject.setText(o.getSubject());
}
if(sender!=null){
sender.setText(o.getFrom());
}
if(datetime!=null){
datetime.setText(o.getDate());
}
}
}
return emailView;
}
}
}
Ok, can you replace your adapter class's getView() method with mine,
#Override
public View getView(int position, View emailView, ViewGroup parent) {
if(emailView == null){
LayoutInflater inflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
emailView= inflater.inflate(R.layout.email_listview_row, null);
}
GetEmailFromDatabase o = itemss.get(position);
if(o!=null){
TextView messageNo = (TextView)emailView.findViewById(R.id.item_number);
TextView subject= (TextView)emailView.findViewById(R.id.emaillistrow_txt_partofemail);
TextView sender= (TextView)emailView.findViewById(R.id.emaillistrow_txt_sender);
TextView datetime= (TextView)emailView.findViewById(R.id.emaillistrow_txt_datetime);
ImageView icon= (ImageView)emailView.findViewById(R.id.emaillistrow_icon_indicator);
icon.setImageResource(R.drawable.y_icon);
if(messageNo!=null){
messageNo.setText(Integer.toString(o.getMessage_No()));
}
if(subject!=null){
subject.setText(o.getSubject());
}
if(sender!=null){
sender.setText(o.getFrom());
}
if(datetime!=null){
datetime.setText(o.getDate());
}
}
return emailView;
}
after replacing let me know what happen..