I am new to android . I have an application working with listview and i want to number each list view item.
Like,
1 list item
2 list item
3 list item
MainActivity.java
public class MainActivity extends AppCompatActivity {
private OrderDbManager orderDbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
final String[] from = new String[] { OrderDbHelper.FOOD_NAME,
OrderDbHelper.QUANTITY, OrderDbHelper.PRICE };
final int[] to = new int[] {R.id.tvFoodName, R.id.tvItemQuantity, R.id.tvItemPrice };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orderDbManager = new OrderDbManager(this);
orderDbManager.open();
final Cursor cursor = orderDbManager.fetch();
listView = (ListView) findViewById(R.id.listView);
listView.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
adapter.notifyDataSetChanged();
LayoutInflater layoutInflater = getLayoutInflater();
listView.addHeaderView(layoutInflater.inflate(R.layout.cart_header,listView, false));
listView.setAdapter(adapter);
}
public class MyAdapter extends SimpleCursorAdapter {
public MyAdapter(Context context, int layout, Cursor c,
String[] from,int[] to) {
super(context, layout, c, from, to);
}
#Override
public int getCount() {
return 0;
}
#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) {
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null, true);
TextView txt= (TextView) convertView.findViewById(R.id.tvSlNo);
txt.setText(position + 1);
return convertView;
}
}
}
activity_main.xml
<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" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
list_item.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="wrap_content"
android:layout_margin="5dp"
android:minHeight="50dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/tvSlNo"
android:layout_width="0dip"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:layout_weight="0.1" />
<TextView
android:id="#+id/tvFoodName"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
<TextView
android:id="#+id/tvItemQuantity"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:textStyle="normal"
android:textColor="#android:color/black"
android:gravity="center"/>
<TextView
android:id="#+id/tvItemPrice"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
</LinearLayout>
i want to set numbering in tvSlNo.
please help
In your getView() method of your listViewAdapter simply use the position variable and set it on your TextView. You may need to add 1 to the position since it starts with 0. So use (position+1) and then set it to your TextView
Though this question may be too late but for other reference purposes.
Check where you have this code: txt.setText(position + 1);
you are close to your solution. You just have to typecast position + 1 to String since setText() only take String alone.
Related
I have a Custom Listview that it is updated from a EditText component in a dialog. I have the custom row, the adapter class and the custom dialog all working but I can't seem to trigger the code in the adatper class that would add the text from the edit text control to the list. Here is my activity code, let me know if you want the adapter code. It worked before I added the custom row and adapter to the list :(
Symptom of problem: emailAdapter.notifyDataSetChanged(); does nothing
public class InvitePlayers_Activity extends Activity {
ListViewAdapter emailAdapter = null;
ImageView imgView_mail;
ImageView imgView_confirm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //remove title bar
setContentView(R.layout.activity_inviteplayers);
//Generate list View from ArrayList
displayListView();
}
private void displayListView() {
//assign controls
final ListView listView = (ListView) findViewById(R.id.listView_invitePlayers);
imgView_mail = (ImageView)findViewById(R.id.imgView_mail);
//Test data
ArrayList<String> inviteNew = new ArrayList<String>();
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
emailAdapter = new ListViewAdapter(this,inviteList);
listView.setAdapter(emailAdapter);
// Assign adapter to ListView
listView.setTextFilterEnabled(true);
//Edit listeners
imgView_mail.setOnClickListener(new View.OnClickListener() {
public void onClick(View view)
{
//variables
final String enteredMail = "testListViewEntry";
final ArrayList<ArrayList<String>> inviteList = new ArrayList<ArrayList<String>>();
ArrayList<String> invite = new ArrayList<String>();
invite.add(0, enteredMail);//add first email
invite.add(1,"icon_invitestatussent.png"); //add first status icon
inviteList.add(invite);
emailAdapter.notifyDataSetChanged();
listView.setAdapter(emailAdapter);
}
});
}
}
Adapter code as requested
public class ListViewAdapter extends BaseAdapter {
private Activity context;
ArrayList<ArrayList<String>> inviteDetails = new ArrayList<ArrayList<String>>();
public ListViewAdapter(Activity context, ArrayList<ArrayList<String>> inviteDetails ) {
this.inviteDetails = inviteDetails;
this.context = context;
}
#Override
public int getCount() {
return inviteDetails.size();
}
#Override
public Object getItem(int i) {
return inviteDetails.get(i).get(0);
}
#Override
public long getItemId(int i) {
return i;
}
public View getView(int position, View view, ViewGroup parent){
//Inflater
LayoutInflater inflater = context.getLayoutInflater();
//get row view
if (view == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.list_item_email, null);
}
//assign controls
final TextView textView_playerEmail = (TextView) view.findViewById(R.id.textView_playerEmail);
ImageView imgView_inviteStatus = (ImageView) view.findViewById(R.id.imgView_inviteStatus);
//Assign control values that are dynamic
textView_playerEmail.setText(inviteDetails.get(position).get(0));
imgView_inviteStatus.setImageResource(R.drawable.icon_invitestatussent);
return view;
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
Custom row xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="#+id/textView_playerEmail"
android:textColor="#color/white"
android:text="item1">
</TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_inviteStatus" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView_remove"
android:src="#drawable/btn_cancel" />
</LinearLayout>
The activity layout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:background="#color/yellow"
android:layout_margin="20dp"
android:padding="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:gravity="left|center">
<ImageView
android:layout_width="45dp"
android:layout_height="34dp"
android:id="#+id/imgView_mail"
android:src="#drawable/btn_mail"
android:layout_weight="0.22"
android:padding="3dp" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView_invitePlayers"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/btn_confirm"
android:src="#drawable/btn_confirm"
android:clickable="false"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:padding="2dp"
android:layout_weight="1" />
</LinearLayout>
</FrameLayout>
Well, for what we discussed, you wanted something like this:
When you want to make a custom ListView, you have to write your own adapter. In this particular case I'm subclassing the BaseAdapter class.
This custom adapter will take hold of my data model, and will inflate the data to my ListView rows.
First, I'll create the XML of my custom row. You can see the code below.
item_mail.xml
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:id="#+id/tv_mail"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iv_icon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_menu_report_image" />
</RelativeLayout>
Here I've created a row that displays a text and an image.
Now, I'll create my custom adapter to handle this XML. As you can see below.
MailAdapter.java
public class MailAdapter extends BaseAdapter {
private static final String LOG_TAG = MailAdapter.class.getSimpleName();
private Context context_;
private ArrayList<ArrayList<String>> mailitems;
public MailAdapter(Context context, ArrayList<ArrayList<String>> mailitems) {
this.context_ = context;
this.mailitems = mailitems;
}
#Override
public int getCount() {
return mailitems.size();
}
#Override
public Object getItem(int position) {
return mailitems.get(position).get(0);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context_.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.item_mail, null);
}
TextView tv_mail = (TextView) convertView.findViewById(R.id.tv_mail);
ImageView iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
String mail = mailitems.get(position).get(0);
String icon = mailitems.get(position).get(1);
Log.d(LOG_TAG,"Mail: " + mail + " mail_icon: " + icon);
tv_mail.setText(mail);
// iv_icon.setImageURI(); Here you can do whatever logic you want to update your image, using URI's, ID's, or something else.
return convertView;
}
}
Ok. Now we have everything to make this work. In your Activity class, do something like that:
activity_main.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:text="#string/hello_world"
android:id="#+id/tv_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/tv_header">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add mail"
android:id="#+id/button"
android:layout_gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity {
private int numMail = 1; // Dummy int to create my items with different numbers.
private MailAdapter mailAdapter; // Your custom adapter.
private ArrayList<ArrayList<String>> mailItems; // This is going to be your data structure, everytime you change it, call the notifyDataSetChanged() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button) findViewById(R.id.button);
ListView lv_mail = (ListView) findViewById(R.id.listView);
mailItems = new ArrayList<>();
mailAdapter = new MailAdapter(this,mailItems);
lv_mail.setAdapter(mailAdapter);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addItem(); // The method I'm using to insert the item. Look for it below.
}
});
}
// Here I'm creating a new ArrayList, and appending it to my 'mailItems' Array. After that, I'm notifying the adapter that my data changed.
private void addItem() {
ArrayList<String> mail = new ArrayList<>();
mail.add(0,"mail " + numMail++);
mail.add(1,"path_to_image"); // Depending on what you want to do, put your path, URI, or whatever other way you want to store that image data.
mailItems.add(mail); // Inserting the data on the ArrayList.
mailAdapter.notifyDataSetChanged(); // Notifying the adapter that my ArrayList was modified.
}
}
This should do the trick.
I guess your problem was that you weren't updating the same ArrayList that was in your custom adapter. That's why when you called notifyDataSetChanged()nothing happened, I mean. You were creating a new ArrayList, which wasn't the same that was in your adapter. So here's what I did... I've made the ArrayList global, and then I've used it in my custom adapter constructor. After that, when the user triggers the onClick() method of my button, I'm inserting some new data on my global Array, and then I'm notifying the adapter that the data changed.
You can read a little more about that here and I've found a similar question here, which you can read as well.
Edit: Another related question, which might be an interesting read.
I am new in android. I want to show the data that I get from Database into ListView.
For now I can show only a single data.
How to show multiple data into custom ListView?
Here's my MainActivity.class
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
Log.d("Reading: ", "Reading all contacts..");
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllAccommodations();
ArrayList <String> allItems2 = new ArrayList<String>();
for (AllItem cn : allItems) {
allItems2.add(cn.getItem_name());
allItems2.add(cn.getAreaNAme());
}
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,allItems2);
listview.setAdapter(adapter);
I have my own custom ListView like this
Acco.xml
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
AccoLayout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Is there anyone can help me with this?
You need to create CustomAdapter class for this.
Use your listview as it is.
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
And your custom Listview also.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Create your custom adapter class and pass data to this class.
adapter = new ListAdapter(this, allItems2);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Here is CustomAdapter class.
public class ListAdapter extends BaseAdapter {
public ListAdapter(Activity a, List <AllItem> allItems) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.AccoLayout, null);
TextView itemName = (TextView)vi.findviewById(R.id.item_name);
TextView areaName = (TextView)vi.findviewById(R.id.area_name);
// Set your data here
itenName.setText(data.get(position));//like this
return vi;
}
}
I assume that your allItems2 list has a pattern like itemName0, areaName0, itemName1, areaName1, itemName2.. etc. Then, you can write a custom adapter like this.
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final List<String> items;
public CustomAdapter (Activity context, List<String> items) {
super(context, R.layout.AccoLayout, items);
this.context = context;
this.items= items;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.AccoLayout, null, true);
TextView itemName = (TextView) rowView.findViewById(R.id.item_name);
TextView areaName= (TextView) rowView.findViewById(R.id.area_name);
itemName.setText(items.get(2*position));
areaName.setText(items.get(2*position + 1));
return rowView;
}
}
Then call it from your main activity;
CustopAdapter adapter = new CustomAdapter (MainActivity.this, allItems2);
listview.setAdapter(adapter);
Also, by Android's convention, try not to use uppercase letters in XML file names to prevent any complication. You may change to acco_layout.xml.
Consider this to be your Cursor: after you fetch from DB
Cursor mCur = db.getAllAccommodations();
in onCreate:
CurAdapter Cur = new CurAdapter(getActivity(), mCur,0);
final ListView lv = (ListView)findViewById(R.id.listAccommodation);
lv.setFastScrollEnabled(true);
lv.setAdapter(Cur);
Then Make a Subclass:
private class CurAdapter extends CursorAdapter{
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.item_name);
TextView tv1 = (TextView) view.findViewById(R.id.area_name);
String item = (cursor.getString(cursor.getColumnIndexOrThrow("ColumnName1")));
String area = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("ColumnName2")));
tv1.setText(item);
tv.setText(area);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.AccoLayout, null);
return view;
}
}
Where R.layout.AccoLayout is your "listview row" layout, for example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_name"
android:textSize="14sp" />
</RelativeLayout>
I want to add an button to the footer of listview, I'm using horizontal listview, The adapter class:
public ItemAdapter(Context context, int layout, Cursor c,String[] from, int[] to) {
super(context, layout, c, from, to,0);
this.layout = layout;
inflator= LayoutInflater.from(context);
}
I return the newView and bindView:
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflator.inflate(layout, parent, false);
TextView footer = (TextView)v.findViewById(R.id.bAdd);
return v;
}
#Override
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(ItemDb.ID));
final String name = c.getString(c.getColumnIndex(ItemDb.NAME));
}
and the activity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = new String[] { ItemDb.NAME, ItemDb.CONDITION, ItemDb.EMAIL};
int[] to = new int[] { R.id.etItemName, R.id.etItemCondition, R.id.etEmail };
itemAdapter = new ItemAdapter(this,R.layout.list_item, null, from, to);
this.setListAdapter(itemAdapter);
}
How can I add the button in listview footer?
Here is my 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" >
<ImageView
android:id="#+id/photo"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/photo_frame"
android:contentDescription="#string/imagedescription"
android:src="#drawable/ic_launcher" >
</ImageView>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:layout_marginLeft="10dp"
android:text="#string/condition"
android:textColor="#000"
android:textSize="14sp" />
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/condition"
android:layout_marginLeft="10dp"
android:text="#string/email"
android:textColor="#000"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
Override getCount() of listview adapter to return data.size()+1 elements;
In getView() insert something like this:
if (position == data.size()){
return getFooterView();
}
In getFooterView() inflate proper layout with button.
I'm trying to get data from sqlite to listview so i followed the example in the answer in this url
Make listview from SQLite database
the CustomCursorAdapter is going like that
public class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.topics, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.listLabel);
if (name_text != null) {
name_text.setText(name);
}
}
}
but onCreate i have this code
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
with xml as follows
<?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:padding="5dp" >
<ImageView
android:id="#+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="#+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="15sp" >
</TextView>
</LinearLayout>
the problem is .. listView is Null!! .. which i don't know why!!
You are refering to your layout topics. You should have a listview defined in xml with a id.
In your activity onCreate() after setCOntentView(R.layout.topics), fins the id of listview and set adapter to your list.
In your xml say topics.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"
android:orientation="vertical"
android:background="#0095FF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/lv">
</ListView>
// other ui elements
</LinearLayout>
Then in your activity onCreate()
setContentview(R.layout.topics);
ListView listView = (ListView)findViewById(R.id.lv);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
From the discussion in chat
EDIT:
Define main.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"
android:orientation="vertical"
android:background="#0095FF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="#android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp"
android:divider="#000000"
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>
In your activivity
public class MainActivity extends Activity {
ListView lv;
CustomCursorAdapter cus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.list);
cus= new CustomCursorAdapter(parameters);
lv.setAdapter(cus);
}
}
Define customCursor adapter inflate a custom layout. Set image for image view and text for textview.
Try to edit your xml
<?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:padding="5dp"
android:id="#+id/main"
>
<ImageView
android:id="#+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="#+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="15sp" >
</TextView>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/topics"
></ListView>
</LinearLayout>
In Activity onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
}
I have to apply pagination concept on ListView my list view contains data parsed from web service. below is code given that how I have displayed data in list view as below.
try {
ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
lv1 = (ListView) findViewById(R.id.lstlodgingresult);
adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view,
new String[] { "Srno", "Names", "URL", "Address1", "Address2", "Telephone", "Category", "PetH",
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1, R.id.txtaddress2, R.id.txtphone, R.id.txtcategory,
R.id.txtpetpolicyH, R.id.txtpetpolicyC }
);
lv1.setScrollbarFadingEnabled(false);
lv1.refreshDrawableState();
lv1.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
you just need to add a Footer View in the Listyou created. Then for the footer view (might be button/image/text) set a ClickListener for that and in Listener add the items into your list and again refresh the activity. I am adding a little tutorial that will help you in this.
I used the following Method for Pagination:
The List Class:
public class customListView extends Activity implements OnClickListener{
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
public EfficientAdapter(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return add_Names.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) {
convertView = mInflater.inflate(R.layout.listcontent, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txt1);
holder.text2 = (TextView) convertView
.findViewById(R.id.txt2);
holder.text3 = (TextView) convertView
.findViewById(R.id.txt3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(add_Names.get(position).toString());
holder.text2.setText(location.get(position).toString());
holder.text3.setText(details.get(position).toString());
return convertView;
}
static class ViewHolder {
TextView text;
TextView text2;
TextView text3;
}
}//end of efficient Adapter Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new EfficientAdapter(this);
l1 = (ListView) findViewById(R.id.ListView01);
View footerView =
((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false);
l1.addFooterView(footerView);
l1.setAdapter(adapter);
mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
more = (Button) footerView.findViewById(R.id.moreButton);
more.setOnClickListener(this);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.moreButton:
//Your code to add some more data into list and then call the following to refresh your lits
adapter.notifyDataSetChanged();
break;
}//end of switch
}//end of onClick
}//end of Custom List view class
layout_footerview.xml:(you can add whatever you link in the footer for the list. I used button you can use Text or image or whatever you want)
<?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="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<Button
android:text="Get more.."
android:id="#+id/moreButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold">
</Button>
</LinearLayout>
</LinearLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</RelativeLayout>
list-content.xml:(modify as u like to be your list row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Test Description" android:textSize="15dip" android:textStyle="bold">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt2" android:layout_below="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Address" android:textSize="10dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt3" android:layout_below="#+id/txt2" android:layout_toRightOf="#+id/image1"
android:text="Details" android:textSize="10dip" ></TextView>
</RelativeLayout>
I Hop this will definetly help you.!
Mark this as true and UpVote; if this helps you.
Thanks
sHaH..