Reputation: 51
every body I am developing a App in which i am getting a issue ( in-fact no nay issue but doesn't work).I am using adapter here
menuList = (ListView) findViewById(R.id.menu_list);
//Cursor cursor = dh.rawQuery("SELECT _id, item_id, item_type , item_name,Item_cost FROM temp_menu WHERE item_name LIKE ? AND Item_cost LIKE ? ",new String[]{"%","%"});
Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp, null,"item_name=?", new String[] {"Apple"}, null,null, null);
startManagingCursor(cursor);
CustAdpt custadapter = new CustAdpt(this, cursor);
menuList.setAdapter(custadapter);
Custdpt is my custom-adapter. This my code of custom-adapter
public class CustAdpt extends CursorAdapter{
private Context mContext;
private LayoutInflater mInflater;
Cursor cursor, c;
View convertView;
private String str_item_id;
private String str_item_type;
private String str_item_name;
private String str_item_cost;
ViewHolder holder;
SQLiteDatabase dh = DatabaseHelpereKOT.getInstance().getDb();
public CustAdpt(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
mContext = context;
cursor = c;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
holder.setImType((ImageView) view.findViewById(R.id.veg_nv_image));
holder.setTvTitle((TextView) view.findViewById(R.id.item_name));
holder.setTvPrice((TextView) view.findViewById(R.id.item_cost));
holder.getTvTitle().setText(cursor.getString(cursor.getColumnIndex("item_name")));
holder.getTvDescription().setText(cursor.getString(cursor.getColumnIndex("Item_cost")));
int _id = cursor.getInt(cursor.getColumnIndex("_id"));
view.setTag(R.id.item_name, _id);
int type = cursor.getInt(cursor.getColumnIndex("item_type"));
if (type == 1) {
//holder.getImType().setPadding(6, 0, 0, 0);
holder.getImType().setBackgroundResource(R.drawable.non_veg);
} else {
holder.getImType().setBackgroundResource(R.drawable.veg);
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final ViewHolder holder;
c = cursor;
convertView = mInflater.inflate(R.layout.list_data_item, parent, false);
holder = new ViewHolder();
holder.setId(cursor.getInt(0));
((ImageView) (convertView
.findViewById(R.id.main_body_item_title_second_pics)))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// System.out.println(view.getTag());
addToDB(holder.getId());
//Toast.makeText(getApplicationContext(), "insertDataOrder method", 2000).show();
System.out.println("i m in ryt [palce");
}
});
convertView.setTag(holder);
return convertView;
}
void addToDB(Integer objId) {
if (objId != null) {
int _id = objId;
Cursor cursor = dh.query(DatabaseHelpereKOT.RESTAURANT_menu_temp,
new String[] { "_id", "item_id", "item_type", "item_name",
"Item_cost"},"_id=?", new String[] { String
.valueOf(_id) }, null, null, null);
if ((cursor != null) && (cursor.getCount() > 0)
&& cursor.moveToFirst())
{
str_item_id = cursor.getString(cursor.getColumnIndex("item_id"));
str_item_type = cursor.getString(cursor
.getColumnIndex("item_type"));
str_item_name = cursor.getString(cursor.getColumnIndex("item_name"));
str_item_cost = cursor.getString(cursor
.getColumnIndex("Item_cost"));
ContentValues orderValues = new ContentValues();
orderValues.put("item_id", str_item_id);
orderValues.put("item_type", str_item_type);
orderValues.put("item_name", str_item_name);
orderValues.put("Item_cost", str_item_cost);
dh.insert(DatabaseHelpereKOT.RESTAURANT_menu_order, null, orderValues);
String msg = "Menu Item Added Successfully";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
}
}
}
public Handler addMenuItemHandler = new Handler() {
@Override
public void handleMessage(android.os.Message msg) {
if (msg.what == 1) {
System.out.println("inside handler");
Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
.show();
}
};
};
}
This is my structure of database-
"create table "
+ RESTAURANT_menu_temp
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,item_id TEXT , item_type TEXT , item_name TEXT , Item_cost DOUBLE)",
"create table "
+ RESTAURANT_menu_order
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,item_id TEXT , item_type TEXT , item_name TEXT , Item_cost DOUBLE)",
My xml file which is used in CustAdpt.java file is as follow
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/veg_nv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/non_veg" />
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/item_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/add_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add_item_order" />
</LinearLayout>
Now this is all code where i am getting problem. Now i am going to describe . First of all data are coming dynamically json format . I parse all these json and save it to sqlite database. I did all these above step successfully . Now i trying to retrieve all these data from database to my emulator with help of Custom Adapter . I am using Custom Adpter because , in app i have to add button in each row and on each button click i have to perform different task. But i ma stuck with problem that data is not retrieve or showing in list-view . I am nor getting any exception neither any error , it is just logical mistake. I am new in android world .so please take me out of this problem.Thanks in advance to all.
Upvotes: 0
Views: 102
Reputation: 1518
I think this is problem of cursor. May be data is not storing in data base.check your cursor.
Upvotes: 0
Reputation: 1420
replace menuList.setAdapter(adapter);
with menuList.setAdapter(custadapter );
You are attaching wrong object.
Upvotes: 1