Android - How using Pinned Section ListView library in project? - android

How I can use from this library in my project ?
Pinned Section ListView
I am fetching my data from sqlite and set in adapter .
StartActivity.java:
public class StartActivity extends AppCompatActivity {
public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/";
public ArrayList<StructNote> notes = new ArrayList<StructNote>();
public ArrayAdapter adapter;
public static String PACKAGE_NAME;
DB db = new DB(StartActivity.this);
public Cursor cursor;
public SQLiteDatabase sql;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
PACKAGE_NAME = getApplicationContext().getPackageName();
File file = new File(DIR_DATABASE + PACKAGE_NAME + "/newTest");
file.mkdirs();
db.GetPackageName(PACKAGE_NAME);
db.CreateFile();
try {
db.CreateandOpenDataBase();
} catch (IOException e) {
e.printStackTrace();
}
sql = db.openDataBase();
final ListView lstContent = (ListView) findViewById(R.id.lstContent);
adapter = new AdapterNote(notes);
lstContent.setAdapter(adapter);
populateListView();
}
public void populateListView() {
try {
cursor = sql.rawQuery("SELECT ContentID as CONID," +
" Pav as Pav," +
" Title as WTitle," +
" Flag as Flag" +
" FROM BookPage" +
" WHERE ArticleID = '" + 61799 + "'order by PageID", null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
StructNote note = new StructNote();
note.CONID = cursor.getInt(cursor.getColumnIndex("CONID"));
note.WTitle = cursor.getString(cursor.getColumnIndex("WTitle"));
note.Flag = cursor.getInt(cursor.getColumnIndex("Flag"));
notes.add(note);
} while (cursor.moveToNext());
}
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
Log.i("xxx", "You have an error");
} finally {
cursor.close();
}
}
}
AdapterNote.java :
public class AdapterNote extends ArrayAdapter<StructNote> {
public AdapterNote(ArrayList<StructNote> array) {
super(G.context, R.layout.dapter_notes, array);
}
public static class ViewHolder {
public TextView txtTitle;
public ViewHolder(View view) {
txtTitle = (TextView) view.findViewById(R.id.txtTitle);
}
public void fill(ArrayAdapter<StructNote> adapter, StructNote item, int position) {
txtTitle.setText(item.WTitle);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
StructNote item = getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.dapter_notes, parent,
false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.fill(this, item, position);
return convertView;
}
}
activity_start.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/LayoutTest">
<ListView
android:id="#+id/lstContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
tools:listitem="#layout/dapter_notes"></ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
dapter_notes.xml :
<?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="vertical"
android:padding="8dip"
android:id="#+id/mainLayout">
<LinearLayout
android:id="#+id/LayoutOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dip">
<LinearLayout
android:id="#+id/LayoutTwo"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title"
android:maxLength="100"
android:singleLine="true"
android:ellipsize="end"
android:gravity="right"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
StructNote.java :
public class StructNote {
int CONID;
String WTitle;
int Flag;
}
I read the guide of this library but I can't use from it.
The value of note.Flag for certain rows is 0 and other certain 1 I need to pinned 0 .

Related

RecyclerView shows Strings but not Images

I have a SQLite database and want to search Items thorugh DB and then showing the items on recyclerView. Everything looking fine but recyclerView not showing the pictures, But somehow It shows the Strings
Normally pictures I saved inside the SQLite as BLOB should be displayed on the left.
Here is SearchFragment (Where I make the search and start recyclerView to display items)
public class SearchFragment extends Fragment {
Button searchbutton;
EditText editText;
View v;
Cursor cursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_search, container, false);
addListenerOnButton(v);
return v;
}
private void addListenerOnButton(View v) {
searchbutton = v.findViewById(R.id.searchfragment_button);
editText = v.findViewById(R.id.searchfragment_edittext);
searchbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String keystring = editText.getText().toString();
if(keystring.isEmpty())
Toast.makeText(getContext(),R.string.empty_search ,Toast.LENGTH_LONG).show();
else
new FoodQuery().execute(keystring);
}
});
}
//Place I start the search on background
private class FoodQuery extends AsyncTask<String, Void, Void>{
#Override
protected Void doInBackground(String [] keys) {
String name_col;
DietAppDatabaseHelper daDBHelper;
SQLiteDatabase daDB;
daDBHelper = new DietAppDatabaseHelper(getContext());
try {
daDB = daDBHelper.getReadableDatabase();
if (Locale.getDefault().getLanguage() == "tr")
name_col = "name_tr";
else
name_col = "name_eng";
String query = "SELECT * FROM food_data WHERE " //Query here
+ name_col + " LIKE ?" ;
cursor = daDB.rawQuery(query, new String[]{"%" + keys[0] + "%"});
viewResults(name_col);
daDB.close();
} catch (SQLiteException e){
Log.d("SearchFragment", "doInBackground: " + e);
}
return null;
}
}
private void viewResults(String lang) {
int name_col = -1; //For language issues not important
if(lang == "name_tr")
name_col = 1;
else if(lang == "name_eng")
name_col = 2;
ArrayList<String> tmpnames = new ArrayList<>(); //I put pictures to byte[] and names to String Arraylist here
ArrayList<byte[]> tmppictures = new ArrayList<>();
if(cursor.moveToFirst()){
tmpnames.add(cursor.getString(name_col));
tmppictures.add(cursor.getBlob(5));
while(cursor.moveToNext()){
tmpnames.add(cursor.getString(name_col));
tmppictures.add(cursor.getBlob(5));
}
}
else {
Looper.prepare();
Toast.makeText(getContext(), R.string.no_food_found, Toast.LENGTH_LONG).show(); //To prevent empty search
}
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
RecyclerView recyclerView = v.findViewById(R.id.recycler_view);
SearchResultAdapter adapter = new SearchResultAdapter(getContext(), tmppictures, tmpnames);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter);
}
});
}
}
Layout File of SearchFragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".fragments.SearchFragment"
android:id="#+id/fragment_search">
<Button
android:id="#+id/searchfragment_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:text="#string/nav_search"
android:textColor="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/searchfragment_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="64dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="#+id/searchfragment_button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="124dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the adapter
public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.ViewHolder>{
private ArrayList<byte[]> foodimages;
private ArrayList<String> foodnames;
private Context context;
public SearchResultAdapter(Context context, ArrayList<byte[]> foodimages, ArrayList<String> foodnames) {
this.foodimages = new ArrayList<>(foodimages);
this.foodnames = new ArrayList<>(foodnames);
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_result_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.foodname.setText(foodnames.get(position));
holder.foodimage.setImageBitmap(BitmapFactory.decodeByteArray(foodimages.get(position), 0, foodimages.size()));
}
#Override
public int getItemCount() {
return foodnames.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView foodimage;
TextView foodname;
RelativeLayout parentLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
foodimage = itemView.findViewById(R.id.food_seacrh_image);
foodname = itemView.findViewById(R.id.food_search_name);
parentLayout = itemView.findViewById(R.id.search_item);
}
}
}
Lastly, the layout of my item to use in recyclerView
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="#+id/search_item">
<ImageView
android:id="#+id/food_seacrh_image"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<TextView
android:id="#+id/food_search_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/food_seacrh_image"
android:textColor="#FFFFFF"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:textSize="17sp"
android:layout_toEndOf="#+id/food_seacrh_image"
android:layout_marginStart="30dp" />
It seems like the problem could be the line:
holder.foodimage.setImageBitmap(BitmapFactory.decodeByteArray(foodimages.get(position), 0, foodimages.size()));
Should probably be:
holder.foodimage.setImageBitmap(BitmapFactory.decodeByteArray(foodimages.get(position), 0, foodimages.get(position).size));
The third param on decodeByteArray should be the length (in bytes) of the image, you are passing in the actual number of images.

RecyclerView is not displaying (Showing)

there is lot of RecyclerViewin my project mostly i don't why that is same code that i privous used bu that not showing my RecyclerView. i can't find problem what is it. so
anybody can help
thanks in advance
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
//inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.activity_artists, container, false);
Artist_reclyerview = v.findViewById(R.id.artist_reclyerview);
artistPojos = new ArrayList<>();
getartist();
artistAdapter = new ArtistAdapter(context, artistPojos);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 2);
Artist_reclyerview.setLayoutManager(mLayoutManager);
Artist_reclyerview.setAdapter(artistAdapter);
return v;
}
public void getartist() {
musicResolver = getActivity().getContentResolver();
musicUri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
//content Url Meida(LIke media,ALbum )
String sortOrder = MediaStore.Audio.Artists.DEFAULT_SORT_ORDER + " ASC";
musicCursor = musicResolver.query(musicUri, null, null, null, sortOrder);
if (musicCursor != null && musicCursor.moveToFirst()) {
int media_id = musicCursor.getColumnIndex
(MediaStore.Audio.Media._ID);
int artistid = musicCursor.getColumnIndex(
MediaStore.Audio.Artists._ID);
int artistname = musicCursor.getColumnIndex
(MediaStore.Audio.Artists.ARTIST);
do {
String Media_id = musicCursor.getString(media_id);
String artistId = musicCursor.getString(artistid);
String artistName = musicCursor.getString(artistname);
Log.d("mediaid", "" + Media_id);
Log.d("artistid", "" + artistId);
Log.d("artistname", "" + artistName);
} while (musicCursor.moveToNext());
}
}
that is RecyclerView xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="#color/viewBg"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/artist_reclyerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
That is RecyclerView adpater class file
public class ArtistAdapter extends RecyclerView.Adapter<ArtistAdapter.Holder> {
ArrayList<ArtistPojo> artistPojos;
Context context;
LayoutInflater inflater;
View v;
public ArtistAdapter(Context c, ArrayList<ArtistPojo> pojos) {
context = c;
artistPojos = pojos;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflate layout
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.activity_artist_adapter, null);
return new ArtistAdapter.Holder(v);
}
#Override
public int getItemCount() {
return artistPojos.size();
}
#Override
public void onBindViewHolder(Holder holder, int position) {
final ArtistPojo artistPojo = artistPojos.get(position);
holder.artist.setText(artistPojo.getArtist());
holder.artist_img.setImageResource(R.drawable.splash);
}
public static class Holder extends RecyclerView.ViewHolder {
TextView artist;
ImageView artist_img, dot;
CardView Artist;
public Holder(View itemView) {
super(itemView);
//find by id to adpater file
artist = itemView.findViewById(R.id.artist_txttitile);
artist_img = itemView.findViewById(R.id.artist_img);
Artist = itemView.findViewById(R.id.card_view);
dot = itemView.findViewById(R.id.overflow);
}
}
}
And There is RecyclerView xml adpater file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/reclerview_adpater"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="20dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="#dimen/card_margin"
android:elevation="3dp"
card_view:cardCornerRadius="#dimen/card_album_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/artist_img"
android:layout_width="match_parent"
android:layout_height="#dimen/album_cover_height"
android:background="?attr/selectableItemBackgroundBorderless"
android:scaleType="fitXY" />
<TextView
android:id="#+id/artist_txttitile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/thumbnail"
android:paddingLeft="#dimen/album_title_padding"
android:paddingRight="#dimen/album_title_padding"
android:paddingTop="#dimen/album_title_padding"
android:textColor="#color/album_title"
android:textSize="#dimen/album_title" />
<TextView
android:id="#+id/count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:paddingBottom="#dimen/songs_count_padding_bottom"
android:paddingLeft="#dimen/album_title_padding"
android:paddingRight="#dimen/album_title_padding"
android:textSize="#dimen/songs_count" />
<ImageView
android:id="#+id/overflow"
android:layout_width="#dimen/ic_album_overflow_width"
android:layout_height="#dimen/ic_album_overflow_height"
android:layout_alignParentRight="true"
android:layout_below="#id/thumbnail"
android:layout_marginTop="#dimen/ic_album_overflow_margin_top"
android:scaleType="centerCrop"
android:src="#drawable/ic_dots" />
</RelativeLayout>
</android.support.v7.widget.CardView>
That is my ArtistPojo class
public class ArtistPojo {
Long Media_Id;
String Artist, ArtistId, Song;
public ArtistPojo(Long media_Id, String artist, String artistId, String song) {
Media_Id = media_Id;
Artist = artist;
ArtistId = artistId;
Song = song;
}
public Long getMedia_Id() {
return Media_Id;
}
public void setMedia_Id(Long media_Id) {
Media_Id = media_Id;
}
public String getArtist() {
return Artist;
}
public void setArtist(String artist) {
Artist = artist;
}
public String getArtistId() {
return ArtistId;
}
public void setArtistId(String artistId) {
ArtistId = artistId;
}
public String getSong() {
return Song;
}
public void setSong(String song) {
Song = song;
}
}
You are not adding any item in artistPojos ArrayList check it
add data in your arraylist like this
do {
String Media_id = musicCursor.getString(media_id);
String artistId = musicCursor.getString(artistid);
String artistName = musicCursor.getString(artistname);
ArtistPojo pojo= new ArtistPojo();
pojo.setData("");// as per your getter and setter method here
artistPojos.add(pojo)
Log.d("mediaid", "" + Media_id);
Log.d("artistid", "" + artistId);
Log.d("artistname", "" + artistName);
} while (musicCursor.moveToNext());
And also change this make your activity_artist_adapter hight android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/reclerview_adpater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">

Android: Custom ArrayAdapter not working, getView() not getting called

I am using CustomArrayAdapter with ListView. But, even after setting setAdapter it is not working. Following are my code snippets:
PastOrders.java Fragment displaying listView
public class PastOrders extends Fragment {
private View view;
private ListView pastOrdersList;
private Context context;
private ArrayList<Order> orders;
public PastOrders() {
}
#Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
view = inflater.inflate(R.layout.fragment_pastorders, container, false);
context = getActivity();
pastOrdersList = (ListView) view.findViewById(R.id.pastOrdersList);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Order");
query.whereEqualTo("user_id", Vars.currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if(e != null) {
Log.e("TAG", e.getLocalizedMessage());
} else {
ArrayList<Order> orders = new ArrayList<Order>();
Log.i("TAG", objects.toString());
for (ParseObject object: objects) {
Order temp = new Order();
temp.orderId = object.getObjectId();
temp.date = object.getCreatedAt();
temp.status = object.getString("status");
Log.i("TAG", "orderID: "+ object.getObjectId() + " temp orderID"+ temp.orderId);
orders.add(temp);
Vars.pastOrders.add(object);
}
Log.i("TAG", "after for loop past orders: "+ orders.toString());
PastOrdersAdapter pastOrdersAdapter = new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
pastOrdersList.setAdapter(pastOrdersAdapter);
}
}
});
return view;
}
#Override
public void onResume() {
super.onResume();
}
}
PastOrdersAdapter.java Custom Adapter
public class PastOrdersAdapter extends ArrayAdapter<Order> {
private Context context;
private ViewHolder holder;
//private ArrayList<Order> orders;
public PastOrdersAdapter(Context context, int resource, List<Order> orders) {
super(context, resource);
// this.orders = orders;
Log.i("TAG", "POA adapter called");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.i("TAG", "getView from POA");
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
convertView = inflater
.inflate(R.layout.past_orders_row, parent, false);
holder = new ViewHolder();
holder.orderId = (TextView) convertView.findViewById(R.id.orderId);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.status = (TextView) convertView.findViewById(R.id.status);
holder.details = (ImageView) convertView.findViewById(R.id.details);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ParseObject order = Vars.pastOrders.get(position);
Log.i("TAG", "order id in adapter: "+ order.getObjectId());
if(order != null) {
holder.orderId.setText(order.getObjectId());
holder.date.setText(getFormattedDate(order.getCreatedAt()));
holder.status.setText(order.getString("status"));
if(order.getString("status").equals("Delivered")) {
holder.status.setTextColor(Color.GREEN);
} else if(order.getString("status").equals("Cancelled")) {
holder.status.setTextColor(Color.RED);
} else {
holder.status.setTextColor(Color.YELLOW);
}
}
return convertView;
}
static class ViewHolder {
TextView orderId;
TextView date;
TextView status;
ImageView details;
}
}
Order.java
public class Order {
public String orderId;
public Date date;
public String status;
}
fragment_pastorders.xml Layout File for Fragment showing ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/pastOrdersList"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
past_orders_row.xml Layout file for each row of listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="150dp"
android:orientation="vertical"
android:id="#+id/LinearLayout01">
<TextView
android:id="#+id/orderId"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/date"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/status"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_next_item"
android:layout_toRightOf="#id/LinearLayout01"
android:layout_centerVertical="true"
android:id="#+id/details"/>
</RelativeLayout>
I have tried printing getCount() as well and it is non-zero. I have tried everything. Kindly help
Thanks,
Arpit
In the constructor of your custom adapter, your forgot to pass your orders parameter to the parent constructor. So your adapter does not know what to display.
You are passing empty list orders to you adapter. And then in adapter you are using different list: Vars.pastOrders.
So your adapter has empty list.
Replace this:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, orders);
with this:
PastOrdersAdapter pastOrdersAdapter =
new PastOrdersAdapter(getActivity(), R.layout.past_orders_row, Vars.pastOrders);

listview does not show anything

i can not figure out the problem my listview suddenly disappeared. I searched solution and tried every possible change in xml file but i still get the empty screen.
xml for listview
<?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="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
listview items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/dcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="drcode"
android:textColor="#000000" />
<TextView
android:id="#+id/dname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:text="docname"
android:textColor="#000000" />
<TextView
android:id="#+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:text="adr"
android:textColor="#000000" />
<TextView
android:id="#+id/cd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/adr"
android:text="class_desc"
android:textColor="#000000" />
<TextView
android:id="#+id/tvshift"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:text="shift"
android:textColor="#000000" />
<TextView
android:id="#+id/tcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_toRightOf="#+id/docname"
android:text="terrcode"
android:textColor="#000000" />
code of listview adapter
public class planningListViewAdapter extends BaseAdapter {
Context context;
public ArrayList<Planning> planArraylist;
private static LayoutInflater inflater = null;
public Activity attdSetupActivity;
public planningListViewAdapter(Context context,ArrayList<Planning> planArraylist) {
this.context = context;
this.planArraylist = planArraylist;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return planArraylist.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_lv_item, null);
}
Planning p = planArraylist.get(position);
TextView tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
TextView tvDrName = (TextView) convertView.findViewById(R.id.dname);
TextView tvTerr = (TextView) convertView.findViewById(R.id.tcode);
TextView tvAdr = (TextView) convertView.findViewById(R.id.a);
TextView tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
//TextView tvSpeciality= (TextView) convertView.findViewById(R.id.speciaility);
TextView tvShift = (TextView) convertView.findViewById(R.id.tvshift);
tvdrCode.setText(p.getDrCode());
tvDrName.setText(p.getDocName());
tvTerr.setText(p.getTerrCode());
tvAdr.setText(p.getAdr());
tvClassDesc.setText(p.getClassDesc());
//tvSpeciality.setText(p.getSpeciality());
tvShift.setText(p.getShift());
return convertView;
}
}
Activity code
public class PlanningList_activity extends Activity{
private Db_sqlite dbHelper;
private planningListViewAdapter dataAdapter;
private ListView listView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.planning_listview);
/* TextView columnHeader1 = (TextView) findViewById(R.id.column_header1);
TextView columnHeader2 = (TextView) findViewById(R.id.column_header2);
TextView columnHeader3 = (TextView) findViewById(R.id.column_header3);
TextView columnHeader4 = (TextView) findViewById(R.id.column_header4);
TextView columnHeader5 = (TextView) findViewById(R.id.column_header5);
TextView columnHeader6 = (TextView) findViewById(R.id.column_header6);
columnHeader1.setText("Plan No");
columnHeader2.setText("Plan Date");
columnHeader2.setText("Mon");
columnHeader2.setText("Dr Code");
columnHeader2.setText("Morn_Even");
columnHeader2.setText("FF_Code"); */
Log.e("on create", "yes");
listView = (ListView) findViewById(R.id.listView1);
dbHelper = new Db_sqlite(PlanningList_activity.this);
new AsyncLoadplanList().execute();
}
public class AsyncLoadplanList extends
AsyncTask<Void, Void, ArrayList<Planning>> {
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(PlanningList_activity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
Log.e("pre execute", "yes");
}
#Override
public ArrayList<Planning> doInBackground(Void... params) {
try {
Log.e("In background", "yee");
return loadData();
} catch (Exception e) {
Log.e("get lost", e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(ArrayList<Planning> result) {
super.onPostExecute(result);
dbHelper.close();
Log.e("data saving", "yoo");
dataAdapter = new planningListViewAdapter(getApplicationContext(),result);
listView.setAdapter(dataAdapter);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(getApplicationContext(), "data loaded",Toast.LENGTH_LONG).show();
}
private ArrayList<Planning> loadData() {
// Load data here from database
return dbHelper.getPlanningList();
}
}
Database method
public ArrayList<Planning> getPlanningList() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<Planning> planningList = new ArrayList<Planning>();
Cursor mCursor = db.query(DOC_PLANNING_TABLE_NAME, null,null,null,null,null,null);
if (mCursor != null && mCursor.moveToFirst()) {
do {
Planning planning = new Planning();
String docCode = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_dr_code));
String planno = mCursor.getString(mCursor.getColumnIndex(DCP_COLUMN_PLAN_NO));
String terrCode = mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_terr_code));
String adr = mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_PLAN_DATE));
String classdesc= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_MON));
String speciality= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_REF_NO));
String shift= mCursor.getString(mCursor
.getColumnIndex(DCP_COLUMN_MORN_EVEN));
planning.setDrCode(docCode);
planning.setDocName(planno);
planning.setTerrCode(terrCode);
planning.setAdr(adr);
planning.setClassDesc(classdesc);
planning.setShift(shift);
planning.setSpeciality(speciality);
} while (mCursor.moveToNext());
mCursor.close();
}
return planningList;
}
I have wasted a lot of time finding the problem but i cannnot understand why it is not showing on the screen. please guide me.
Correct in your code as below:
#Override
public Object getItem(int arg0) {
return planArraylist.get(planArraylist.get(arg0-1)); }
#Override
public long getItemId(int arg0) {
return arg0; }
I think you write you getView method differently and use a holder class, as shown below:
#Override
public Object getItem(int arg0) {
return planArraylist.get(arg0);
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.single_lv_item, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView) convertView.findViewById(R.id.dcode);
holder.tvDrName = (TextView) convertView.findViewById(R.id.dname);
holder.tvTerr = (TextView) convertView.findViewById(R.id.tcode);
holder.tvAdr = (TextView) convertView.findViewById(R.id.a);
holder.tvClassDesc = (TextView) convertView.findViewById(R.id.cd);
holder.tvShift = (TextView) convertView.findViewById(R.id.tvshift);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Planning p = planArraylist.get(position);
holder.tvdrCode.setText(p.getDrCode());
holder.tvDrName.setText(p.getDocName());
holder.tvTerr.setText(p.getTerrCode());
holder.tvAdr.setText(p.getAdr());
holder.tvClassDesc.setText(p.getClassDesc());
holder.tvShift.setText(p.getShift());
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvTerr;
TextView tvAdr;
TextView tvClassDesc;
TextView tvShift;
}
Also, your list view xml file uses a Linear layout but you have properties that can only be found in a Relative layout such as android:layout_alignParentTop="true". Could you fix that as well?

Modify code to use ArrayList instead of SimpleCursor

Hi this is the code I'm looking at:
https://github.com/findup/Android_Sample_TodoApp/blob/master/src/jp/co/example/testapp/MainActivity.java
Around line 127, it chooses to use a database connection to fetch content from the database.
Instead of fetching data from the database, I'd like to use an ArrayList to hold the data. Could anyone help me figure out what I need to do? Thanks!
Not a one-for-one example based on your code, but this is will populate a list from a database table without a simple cursor:
public class MyListAdapter extends ListActivity {
List<ContactGroup> groups = new ArrayList<ContactGroup>();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.groups = getGrpList();
ContactGroupAdapter cAdapter = new ContactGroupAdapter(this);
setListAdapter(cAdapter);
}
private List<ContactGroup> getGrpList(){
List<ContactGroup> grps = new ArrayList<ContactGroup>();
ContentResolver cr = getContentResolver();
Cursor groupCur = cr.query(Groups.CONTENT_URI, new String [] {Groups._ID, Groups.NAME}, null, null, Groups.NAME + " ASC");
if (groupCur.getCount() > 0) {
while (groupCur.moveToNext()) {
ContactGroup newGroup = new ContactGroup();
newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
grps.add(newGroup);
}
}
return grps;
}
public class ContactGroupAdapter extends BaseAdapter{
public ContactGroupAdapter(Context c) {
mContext = c;
}
public int getCount() {
return groups.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater vi = LayoutInflater.from(this.mContext);
convertView = vi.inflate(R.layout.two_line_list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else {
//Get view holder back
holder = (ViewHolder) convertView.getTag();
}
ContactGroup cg = groups.get(position);
if (cg != null) {
//Name
holder.toptext = (TextView) convertView.findViewById(R.id.text1);
holder.toptext.setText(cg.Name);
//ID
holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
holder.bottomtext.setText(cg.Id);
}
return convertView;
}
private Context mContext;
}
public static class ViewHolder {
TextView toptext;
TextView bottomtext;
}
public class ContactGroup{
public String Id;
public String Name;
}
}
Then there are XML files ...
two_line_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="#+id/text1" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/text2" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
and main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="No Groups" />
</LinearLayout>

Categories

Resources