gridview didn't work second times - android

i have a gridview like a listview. It work correctly first run, but when press back and return the activity that have gridView, get some errors...
logCat: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=761 (# cursors opened by this proc=761)
i look at question like that, solution is always about cursor. I close cursor, when populate items info.. but it didnt work...
my gridView code:
private void refreshList(String sql)
{
gridArray = new ArrayList<Stock>();
final Cursor cursor = _SQLite.RawQueryTry(sql, null);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
if (cursor != null)
{
if (cursor.moveToFirst())
{
for (int i = 0; i < cursor.getCount(); i++)
{
String stockName = cursor.getString(cursor.getColumnIndex("STOK_ADI"));
String stockNo = cursor.getString(cursor.getColumnIndex("STOK_NO"));
String stockCode = cursor.getString(cursor.getColumnIndex("STOK_KODU"));
String stockEntity = cursor.getString(cursor.getColumnIndex("BIRIM"));
String stockKdvOranı = cursor.getString(cursor.getColumnIndex("KDV_ORANI"));
String stockRatio = TableUtils.getFieldValue("KATSAYI", "BIRIM", stockEntity, "STOKBIRI");
String stockAmount = cursor.getString(cursor.getColumnIndex("MIKTAR"));
gridArray.add(new Stock(stockName, stockNo, stockCode, stockKdvOranı, stockEntity, stockAmount, stockRatio, processNo));
cursor.moveToNext();
}
}
}
cursor.close();
gridAdapter = new AdapterStockGridListView(this, R.layout.stockgridlistitems, gridArray);
gridView.setAdapter(gridAdapter);
}
my adapter class is here:
public class AdapterStockGridListView extends ArrayAdapter<Stock>
{
Context context;
int id;
ArrayList<Stock> stock = new ArrayList<Stock>();
public AdapterStockGridListView(Context context, int id, ArrayList<Stock> stock)
{
super(context, id, stock);
this.id = id;
this.context = context;
this.stock = stock;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
RecordHolder holder = null;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(id, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.stockName);
holder.txtStockNo = (TextView) row.findViewById(R.id.stockNo);
holder.txtStockCode = (TextView) row.findViewById(R.id.stockCode);
row.setTag(holder);
}
else
{
holder = (RecordHolder) row.getTag();
}
Stock item = stock.get(position);
holder.txtTitle.setText(item.getStockName());
holder.txtStockNo.setText(item.getStockNo());
holder.txtStockCode.setText(item.getStockCode());
return row;
}
}
static class RecordHolder
{
TextView txtTitle;
TextView txtStockNo;
TextView txtStockCode;
}

This is because you are trying to access closed cursor. So remove this line
cursor.close();
And to manage cursor properly write this line in your activity or fragment
In Activity
startManagingCursor(pass Your Cursor object here);
In Fragment
getActivity().startManagingCursor(pass Your Cursor object here);

override this method in adapter class and also print the size of array may be it has only one vale
#Override
public int getCount() {
return stock.size();
}

Related

Android: fill list view with data from sqlite

Overview: The programme I'm now trying to make has the following steps.
get the input data from users and store them in the SQLite database.
Fill the list view with the data retrieved from the database.
In order to implement 1, I first created the addNewItem() method as follows.
public void addNewPost(Post post) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
// get the variables which are to be put into the database row.
String uploader = post.getUploader();
String content = post.getContent();
String privacy_level = post.getPrivacyLevel();
String uploaded_at = post.getUploadedAt();
String edited_at = post.getEditedAt();
values.put(KEY_UPLOADER, uploader);
values.put(KEY_CONTENT, content);
values.put(KEY_PRIVACY_LEVEL, privacy_level);
values.put(KEY_UPLOADED_AT, uploaded_at);
values.put(KEY_EDITED_AT, edited_at);
db.insert(TABLE_POST, null, values);
db.close();
}
And for 2, I created another method called initListView().
private void initListView() {
SQLiteDatabase db = sqliteHandler.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + SQLiteHandler.TABLE_POST, null);
username.clear();
uploaded_at.clear();
content.clear();
if(cursor.moveToFirst()) {
do {
username.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_UPLOADER)));
uploaded_at.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_UPLOADED_AT)));
content.add(cursor.getString(cursor.getColumnIndex(DataKeyLists.KEY_CONTENT)));
} while(cursor.moveToNext());
}
FeedAdapter adapter = new FeedAdapter(this, username, uploaded_at, content);
listView.setAdapter(adapter);
cursor.close();
}
And finally this is the FeedAdapter class.
public class FeedAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> username;
private ArrayList<String> uploaded_at;
private ArrayList<String> content;
public FeedAdapter(Context context, ArrayList<String> username, ArrayList<String> uploaded_at, ArrayList<String> content) {
this.context = context;
this.username = username;
this.uploaded_at = uploaded_at;
this.content = content;
}
public int getCount() {
return username.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater;
if(convertView == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.feed_item, null);
ImageView ivFeedThumbnail = (ImageView) convertView.findViewById(R.id.ivFeedThumbnail);
TextView tvFeedUsername = (TextView) convertView.findViewById(R.id.tvFeedUsername);
TextView tvFeedUploaded = (TextView) convertView.findViewById(R.id.tvFeedCreated);
TextView tvFeedText = (TextView) convertView.findViewById(R.id.tvFeedText);
TextView tvFeedLikes = (TextView) convertView.findViewById(R.id.tvFeedLikes);
TextView tvFeedComments = (TextView) convertView.findViewById(R.id.tvFeedComments);
Button btnLike = (Button) convertView.findViewById(R.id.btnLike);
Button btnComment = (Button) convertView.findViewById(R.id.btnComment);
tvFeedUsername.setText(username.get(position));
tvFeedUploaded.setText(uploaded_at.get(position));
tvFeedText.setText(content.get(position));
}
return convertView;
}
}
The code seems to have no problem, but when I run the application, the list is still empty.
Any tips for the solution will be very much appreciated.

Android AdapterView cannot display database records in some device

I would like to ask some question about AdapterView.
In my application, there is an activity which retrieve data from database and display them in AdapterView.
However, when i install the application in different devices, I found that the part I have just mentioned could only function on some devices. The others cannot show the database results.
Here is my code:
private void showResults(String query) {
Cursor cursor = searchCustByInputText(query);
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[] {
"cust_code",
"chinese_name"};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] {
R.id.scust_code,
R.id.schinese_name};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter customers = new SimpleCursorAdapter(this,R.layout.cust_list_item, cursor, from, to);
mListView.setAdapter(customers);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = (Cursor) mListView.getItemAtPosition(position);
String cust_code = c.getString(c.getColumnIndex("cust_code"));
if (callFromAct.equals("Main")) {
String pay_term = c.getString(c.getColumnIndex("pay_term"));
String chinese_name = c.getString(c.getColumnIndex("chinese_name"));
String english_name = c.getString(c.getColumnIndex("english_name"));
String address_1 = c.getString(c.getColumnIndex("address_1"));
String address_2 = c.getString(c.getColumnIndex("address_2"));
String address_3 = c.getString(c.getColumnIndex("address_3"));
String address_4 = c.getString(c.getColumnIndex("address_4"));
String contact = c.getString(c.getColumnIndex("contact"));
String telephone = c.getString(c.getColumnIndex("telephone"));
String last_order_date = c.getString(c.getColumnIndex("last_order_date"));
//Pass data to another Activity
Intent it = new Intent(CustEnqActivity.this, CustEnqDetailsActivity.class);
Bundle bundle = new Bundle();
bundle.putString("cust_code", cust_code);
bundle.putString("pay_term", pay_term);
bundle.putString("chinese_name", chinese_name);
bundle.putString("english_name", english_name);
bundle.putString("address_1", address_1);
bundle.putString("address_2", address_2);
bundle.putString("address_3", address_3);
bundle.putString("address_4", address_4);
bundle.putString("contact", contact);
bundle.putString("telephone", telephone);
bundle.putString("last_order_date", last_order_date);
it.putExtras(bundle);
startActivity(it);
}
else {
returnToCallingAct(cust_code);
}
//searchView.setQuery("",true);
}
});
}
}
Besides, I discovered there were two warnings in my logcat.
The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated
AdapterView is a raw type. References to generic type AdapterView should be parameterized
Are they related to the problem?
Try to create a class that extends BaseAdapter and use ViewHolders for performance
eg:
public class MyBaseAdapter extends BaseAdapter {
ArrayList<ListData> myList = new ArrayList<ListData>();
LayoutInflater inflater;
Context context;
public MyBaseAdapter(Context context, ArrayList<ListData> myList) {
this.myList = myList;
this.context = context;
inflater = LayoutInflater.from(this.context); // only context can also be used
}
#Override
public int getCount() {
return myList.size();
}
#Override
public ListData getItem(int position) {
return myList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());
return convertView;
}
// or you can try better way
private TextView detail(View v, int resId, String text) {
TextView tv = (TextView) v.findViewById(resId);
tv.setText(text);
return tv;
}
private ImageView detail(View v, int resId, int icon) {
ImageView iv = (ImageView) v.findViewById(resId);
iv.setImageResource(icon); //
return iv;
}
private class MyViewHolder {
TextView tvTitle, tvDesc;
ImageView ivIcon;
}
}
More info/example:
http://www.pcsalt.com/android/listview-using-baseadapter-android/#sthash.lNGSCiyB.dpbs

ListView showing only one item or last item

The title of this question is same but technical issue are different.
Hi i am trying to get data from SQLite but i am able to show only last item in listview. I tried different- different solution but not getting success.
Problem is not getting item from SQLite(I am able to fetch all item) but showing item using adapter in listview.
Here is my code.
ListActivity.java
db=new DBHelper(getBaseContext());
db.getWritableDatabase();
try {
final DBHelper m = new DBHelper(getBaseContext());
final List<GetSet> NotesWiseProfile = m.getBabyDetails();
for (final GetSet cn : NotesWiseProfile) {
counter++;
String babyName = cn.getBabyName();
String babyImage = cn.getBabyImage();
int babyId = cn.getBabyId();
BabyData baby_data[] = new BabyData[]
{
new BabyData(R.drawable.ic_launcher, babyName,babyId),
};
adapter = new MobileArrayAdapter(this,
R.layout.list_row, baby_data);
listView1.invalidateViews();
listView1.setAdapter(adapter);
}
}
catch (Exception e) {
}
BabyData.java
public class BabyData {
public int icon;
public String title;
public int babyid;
public BabyData(){
super();
}
public BabyData(int icon, String title,int babyId) {
super();
this.icon = icon;
this.title = title;
babyid = babyId;
}
}
MobileArrayAdapter.java
public class MobileArrayAdapter extends ArrayAdapter<BabyData>{
Context context;
int layoutResourceId;
BabyData data[] = null;
public MobileArrayAdapter(Context context, int layoutResourceId, BabyData[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DataHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DataHolder ();
holder.imgIcon = (ImageView)row.findViewById(R.id.imvBabyFace);
holder.txtTitle = (TextView)row.findViewById(R.id.tvbabyNameList);
holder.txtBabyId = (TextView)row.findViewById(R.id.tvBabyId);
row.setTag(holder);
}
else
{
holder = (DataHolder )row.getTag();
}
BabyData weather = data[position];
holder.txtTitle.setText(weather.title);
holder.txtBabyId.setText(String.valueOf(weather.babyid));
holder.imgIcon.setImageResource(weather.icon);
return row;
}
static class DataHolder
{
ImageView imgIcon;
TextView txtTitle;
TextView txtBabyId;
}
}
I don't understand what's wrong in my code. Please give me any hint or reference.
Thanks in Advance.
Put the listview declarations out of the for loop, something like:
BabyData baby_data[] = new BabyData[NotesWiseProfile.size()];
for (final GetSet cn : NotesWiseProfile) {
String babyName = cn.getBabyName();
String babyImage = cn.getBabyImage();
int babyId = cn.getBabyId();
baby_data[counter] = new BabyData(R.drawable.ic_launcher, babyName,babyId);
counter++;
}
adapter = new MobileArrayAdapter(this,
R.layout.list_row, baby_data);
listView1.invalidateViews();
listView1.setAdapter(adapter);
I think you should use a field for storing you babies. Currrently, you are using a local Baby array for that. As far as I know, the ListView always gets its data from the array you passed to it (invalidating it causes the ListView to look up that data again.
To recap: Store your array as a field - if data changes, update the array and call notifyDatasetChanged() on your adapter, which will cause your ListView to reload the data.

retrieve data from sqlite to perform edit/delete by clicking an item in custom listview with 3 fields/columns

sir, can you help me on how i can display the details from a sqlite by clicking its corresponding custom listview item? my listview contains three columns so i wanted it to be displayed in another activity where i can edit the information and delete, etc.. at first, i thought of using row id but gave up on the idea because if i tried to delete an item in database, i cannot use the deleted row id. so the row ids of listview and database won't match. for ex. row ids from database 1,2,3... if i deleted row 2, if i add another item, it will just add to row 4... like this row 1,3,4. thanks for help in advance
public class CustomListView extends Activity {
final Context context = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GroupDb info = new GroupDb(this);
info.open();
ArrayList<Contact> searchResults = info.getView();
final ListView lv = (ListView) findViewById(R.id.srListView);
lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
info.close();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
here is my custombaseadapter
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Contact> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
holder.status = (TextView) convertView.findViewById(R.id.status);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber());
holder.status.setText(searchArrayList.get(position).getStatus());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtPhone;
TextView status;
}
}
here is how i saved the database values with three fields/columns from GroupDb.class in my customlistview
public ArrayList<Contact> getView()
{
// TODO Auto-generated method stub
ArrayList<Contact> results = new ArrayList<Contact>();
String[] columns = new String[]{KEY_NAME, KEY_NUMBER, KEY_STATUS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String sName = "";
String nName = "";
String status = "";
int iName = c.getColumnIndex(KEY_NAME);
int iNumber = c.getColumnIndex(KEY_NUMBER);
int iStatus = c.getColumnIndex(KEY_STATUS);
Contact contact;
for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext())
{
contact = new Contact();
sName += c.getString(iName);
nName += c.getString(iNumber);
status += c.getString(iStatus);
contact.setName(sName);
contact.setPhoneNumber(nName);
contact.setStatus(status);
results.add(contact);
sName = "";
nName = "";
status = "";
}
return results;
}
You are maintaining arraylist of contacts. On click of list view get the position fetch contact object from that given position from your arraylist. You must have to save primary key column in your COntact Class.

ListView alphabetically arrange?

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.

Categories

Resources