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);
}
Related
I want to custom each line of List view is Button with the text view inside it .
But it's not work!
It's only show a button that i have created in the first layout .
What can i do for fix ? Many thanks
Here's my code :
public class MainActivity extends AppCompatActivity {
Button button;
ListView lv ;
ListViewAdapter listViewAdapter;
ArrayList<String> al ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lstView);
button = (Button)findViewById(R.id.button);
al = new ArrayList();
al.add("cho");
al.add("phuong");
al.add("hello");
al.add("bye");
al.add("love");
listViewAdapter= new ListViewAdapter(this , R.layout.button , al);
lv.setAdapter(listViewAdapter);
This is adapter :
public class ListViewAdapter extends ArrayAdapter {
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
*/
Activity context;
int layResID;
ArrayList<String> object;
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource);
this.context=context;
this.layResID = resource;
this.object = object;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(layResID, null);
}
Button btn = (Button)convertView.findViewById(R.id.view);
String string = object.get(position);
btn.setText(string);
return convertView;
}
}
This is main xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.macbook.note.MainActivity"
android:background="#1A237E"
android:orientation="vertical"
android:weightSum="10"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="10dp"
android:background="#drawable/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/lstView"
android:divider="#1A237E"
android:dividerHeight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</ScrollView>
</LinearLayout>
</LinearLayout>
This is custom layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/view"
android:layout_width="300dp"
android:layout_height="100dp"
android:background="#drawable/btn_list"
android:textSize="10dp"/>
</LinearLayout>
Change your ArrayAdapter's super call to the following
super(context, object);
I guess the problem is that you do not have specified the current count of your items which should be created inside of list view. You have two options:
1. Call other constructor of ArrayAdapter so ArrayAdapter class will handle it for you:
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource, object);
}
2. Override ArrayAdapater.getCount() inside your adapter class:
#Override
public int getCount() {
return this.object.size();
}
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.
My app has listview inside dialogueFragment , when user chose item it should Toast , in my case items unclickable .
DialogueFragment
public class HistoryDialogue extends DialogFragment {
ListView list ;
Cursor c ;
HistoryAdapter adapter ;
Context context ;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.history,null);
list = (ListView)view.findViewById(R.id.listView);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
ContentResolver resolver = getActivity().getContentResolver();
c=resolver.query(Contract.SaveRunning.CONTENT_URI,null,null,null,null);
adapter= new HistoryAdapter(getActivity(),c,0);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"keep going ",Toast.LENGTH_LONG).show();
}
});
builder.setView(list);
return builder.create();
}
}
CursorAdapter
public class HistoryAdapter extends CursorAdapter {
public HistoryAdapter(Context context, Cursor c, int flags) {
super(context,c, flags);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.historylist, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView text = (TextView) view.findViewById(R.id.text);
String body = cursor.getString(cursor.getColumnIndex("name"));
text.setText(body);
}
}
history.xml
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal" />
historylist.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text"
android:layout_margin="5dp"
android:layout_weight="1"/>
<Button
android:layout_width="30dp"
android:layout_height="35dp"
android:background="#drawable/delete"
android:id="#+id/btn"
android:layout_margin="10dp"
android:layout_weight="0"
/>
Add android:focusable="false" in your ListView (in history.xml ) and Item main layout (in historylist.xml).
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal" />
and
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp">
...
Add this line to your CursorAdapter's bindView:
((ViewGroup)row).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
if it isn't enough, add clickable="false" to your historylist.xml's Button
I want to create a ListView populated by my own layout and my data. So I looked at examples and I found that I need to extend ArrayAdapter<>.
So I made a CustomAdapter and did everything by following an example.
There is no error in the code but the ListView #id/dashboard doesn't appear in my MainActivity.
I can see the TextView from activity_main.xml but not the ListView.
The sliding menu of the DrawerLayout is working too.
I inflate overview.xml in my CustomAdapter.
Can you give me an hint why it's not working? I'm working on it for hours.
Thank's
CustomAdapter
public class CustomAdapter extends ArrayAdapter<Ssoverview> {
private int layoutId;
private Context context;
private ArrayList<Ssoverview> list;
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
v = (RelativeLayout) ((Activity)context).getLayoutInflater().inflate(this.layoutId, null); //, parent, false);
}
Ssoverview ss = this.list.get(position);
if (ss != null) {
TextView titleTV=(TextView) v.findViewById(R.id.title);
titleTV.setText(ss.getTitle());
TextView authorTV=(TextView) v.findViewById(R.id.author);
authorTV.setText(ss.getAuthor());
}
Log.d("Deb", "Fabrication de la View : "+position);
return v;
}
}
activity_main.xml :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ole" />
<ListView
android:id="#+id/dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
overview.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/grey"
android:layout_marginTop="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre" />
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:text="Note : x/5 y votes" />
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:text="John Doe" />
</RelativeLayout>
And the MainActity where I set the adapter
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.dashboard);
//Declaration factices des Ssoverview
ArrayList<Ssoverview> ssList = new ArrayList<Ssoverview>();
Ssoverview ss1 = new Ssoverview("Dune", "David", "4");
Ssoverview ss2 = new Ssoverview("Le petit poucet", "MLP", "2");
Ssoverview ss3 = new Ssoverview("Locked", "Hervé", "4,2");
ssList.add(ss1);
ssList.add(ss2);
ssList.add(ss3);
Log.d("D", ssList.get(0).getTitle());
//Declaration de l'adapter avec le layout et la ssList
CustomAdapter adapter = new CustomAdapter(this, R.layout.overview, ssList);
lv.setAdapter(adapter);
Log.d("D","Fin de onCreate");
}
}
change
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
to
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource , ssList); <-- change this line
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
in arrayAdapter class super in constructor handle size of your list, as you don't pass your data this set to 0 in default so getView not called at all
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.