Hi I want to resize my listview in android.
I have other source than previous topics.
.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="3dp" >
<TextView
android:id="#+id/currentDirectoryTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Current directory:" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:textSize="30sp"
android:layout_weight="1" />
</LinearLayout>
.class
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.activity_list_item, android.R.id.text1, values);
setListAdapter(adapter);
How to resize my list? It is always small. Same after changing textSize.
This is whole class ListFileActivity.
public class ListFileActivity extends ListActivity {
private String path;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Use the current directory
path = android.os.Environment.getExternalStorageDirectory()
+ File.separator + AppConstant.DIRECTORY;
if (getIntent().hasExtra("path")) {
path = getIntent().getStringExtra("path");
}
updateCurrentDirectoryTextView();
// Read all files sorted into the values-array
List values = new ArrayList();
File dir = new File(path);
if (!dir.canRead()) {
setTitle(getTitle() + " (inaccessible)");
}
String[] list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.startsWith(".")) {
values.add(file);
}
}
}
Collections.sort(values);
// Put the data into the list
/* ListView lst = new ListView(this);
//String[] arr = {"Item 1","Item 2"};
ArrayAdapter<String> ad = new ArrayAdapter<String>(this,R.layout.mylist,values);
lst.setAdapter(ad);*/
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.activity_list_item, android.R.id.text1, values);
setListAdapter(adapter);
}
private void updateCurrentDirectoryTextView() {
((TextView) this.findViewById(R.id.currentDirectoryTextView))
.setText("Current directory: " + path);
}
}
Remove the weight, i.e. remove this line
android:layout_weight="1"
Also what you are trying to do by setting a text size to listView? you should do it with adapter or individual item layouts
The line
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.activity_list_item, android.R.id.text1, values);
Is responsible for setting the adapter for every list element.
What determines the size is the list element layout that is inflated by the adapter, not the ListView itself. In your case, that is android.R.id.text1, which is defined by the system. Therefore, if you want to change it, you need to create your own adapter. This is from an example:
public class MyDataAdapter extends ArrayAdapter<MyData>
{
MyDatadata[] = null;
public MyDataAdapter(Context context, int layoutResourceId, MyData[] data)
{
super(context, layoutResourceId, data);
this.data = data;
}
public MyDataAdapter(Context context, int layoutResourceId, List<MyData> data)
{
super(context, layoutResourceId, data);
this.data = data.toArray(new MyData[data.size()]);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MyDataHolder holder = null;
if (row == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MyDataHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.mydata_1);
holder.txtTitle2 = (TextView) row.findViewById(R.id.mydata_2);
holder.txtTitle3 = (TextView) row.findViewById(R.id.mydata_3);
holder.txtTitle4 = (TextView) row.findViewById(R.id.mydata_4);
row.setTag(holder);
}
else
{
holder = (MyDataHolder) row.getTag();
}
MyData mydata= data[position];
holder.txtTitle.setText(mydata.getName());
holder.txtTitle2.setText(mydata.getType());
holder.txtTitle3.setText(mydata.getDate());
holder.txtTitle4.setText(mydata.getStatus());
return row;
}
static class MyDataHolder
{
TextView txtTitle;
TextView txtTitle2;
TextView txtTitle3;
TextView txtTitle4;
}
#Override
public MyData getItem(int position)
{
return data[position];
}
The item xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/mydata_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:textAppearance="#android:attr/textAppearanceLarge"
android:textColor="#736F6E"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/mydata_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_below="#id/mydata_1"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="#736F6E"
android:layout_alignParentLeft="true"
android:textAppearance="#android:attr/textAppearanceLarge"
/>
<TextView
android:id="#+id/mydata_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_below="#id/mydata_2"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="#736F6E"
android:layout_alignParentLeft="true"
android:textAppearance="#android:attr/textAppearanceLarge"
/>
<TextView
android:id="#+id/mydata_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_below="#id/mydata_3"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="#736F6E"
android:layout_alignParentLeft="true"
android:textAppearance="#android:attr/textAppearanceLarge"
/>
</RelativeLayout>
And use it like this:
ArrayAdapter<MyData> adapter = new MyDataAdapter(inflater.getContext(), R.layout.listelement_mydata, mydatas);
setListAdapter(adapter);
Later I needed to change this to a SimpleCursorAdapter though because of changes in stuff, which looked like this:
Cursor c = getMyDataDataSource().getMyDatasCursor();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(inflater.getContext(),
R.layout.listelement_mydata, c, new String[] { "name", "type", "date", "status" }, new int[] {
R.id.mydata_1, R.id.mydata_2, R.id.mydata_3, R.id.mydata_4 }, 0);
setListAdapter(adapter);
But you needed an ArrayAdapter so that's the one you will need.
EDIT:
....this might have been a stupidly bloated answer, considering you can probably use a single-textview List Element XML in place of android.R.id.text1, without your own adapter.
I won't remove it because if you need anything more complicated than a single textview then it might come in handy, but... yeah. Just define a single element XML for the list view with a single text view in it and it should work.
Related
My xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/address" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:layout_below="#+id/address"
android:id="#+id/win_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/win_list"
android:layout_below="#+id/win_title"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/lose_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lose_list"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text=""
android:id="#+id/cannot_compare_title" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cannot_compare_list"
android:layout_weight="1" />
</LinearLayout>
And this is my custom Adapter:
public class AnalysisResultAdapter extends ArrayAdapter<ComparisonResult> {
static class ViewHolder {
TextView address;
TextView win_title;
ListView win_list;
TextView lose_title;
ListView lose_list;
TextView cannot_compare_title;
ListView cannot_compare_list;
}
private Hashtable<String, SevenEleven[]> sevenElevenData;
public AnalysisResultAdapter(Context context, int resource, List<ComparisonResult> houses) {
super(context, resource, houses);
}
public void setSevenElevenData(Hashtable<String, SevenEleven[]> sevenElevenData) {
this.sevenElevenData = sevenElevenData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.analysis_result_list_item, null);
holder = new ViewHolder();
holder.address = (TextView) view.findViewById(R.id.address);
holder.win_title = (TextView) view.findViewById(R.id.win_title);
holder.win_list = (ListView) view.findViewById(R.id.win_list);
holder.lose_title = (TextView) view.findViewById(R.id.lose_title);
holder.lose_list = (ListView) view.findViewById(R.id.lose_list);
holder.cannot_compare_title = (TextView) view.findViewById(R.id.cannot_compare_title);
holder.cannot_compare_list = (ListView) view.findViewById(R.id.cannot_compare_list);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if (this.sevenElevenData.size() == 0) {
return view;
}
ComparisonResult result = getItem(position);
holder.address.setText(result.house.Address);
if (result.win.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.win) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.win_title.setText("win");
holder.win_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.lose.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.lose) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.lose_title.setText("lose");
holder.lose_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
if (result.cannotBeCompared.size() > 0) {
ArrayList<String> storeNames = new ArrayList<String>();
for (int storeIndex : result.cannotBeCompared) {
storeNames.add(this.sevenElevenData.get(result.house.Area)[storeIndex].StoreName);
}
holder.cannot_compare_title.setText("cannotBeCompared");
holder.cannot_compare_list.setAdapter(new ArrayAdapter<String>(
getContext(),
R.layout.text_list,
R.id.text_list,
storeNames
));
}
return view;
}
}
The problem is: Only the #+id/address TextView shows the text successfully, other things are not shown. But I don't know why this happened.
How can I solve this problem ? Can someone help me ?
Thanks.
Use ScrollView intead of outer(parent) ListView.
And use LisView for the inner ListView.
It gives you the scrolling feature.
1/ layout_below only works in RelativeLayout, you are using a LinearLayout with horizontal orientation so all the elements will be rendered side by side, and all elements have the layout_width:"match_parent" so only the first element will be shown.
2/ when you use a ListView in another scrollView the renderer can't calculate the height of the second one
I have a listview which contents of different post. My current post limit per page load is 25. Each post is of different size. I want ListView size upto those 25 objects, i.e., it should accomodate all the objects from listadapter.
I have currently set my listview size as following.
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="5000sp" >
</ListView>
My ListAdapter is set as:
ListAdapter adapter = new SimpleAdapter(NewsFeed.this, contactList,
R.layout.list_item, new String[] { TAG_MESSAGE,
TAG_CREATETIME }, new int[] { R.id.message,
R.id.time });
setListAdapter(adapter);
list_item.xml
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/time"
android:gravity="right"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#b1b1b1"
android:background="#abc0e3" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/cover" />
<TextView
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:text="TextView"
android:linksClickable="true"
android:autoLink="web" />
</LinearLayout>
see if this works for you, where you adjust the width then you can decide on what width to use based on the text length,
/* create an inline class that defines each item in the list
private class Post
{
String time;
String message;
Bitmap bmp;
public String getTime(){return time;}
public String getMessage(){return message;}
public Bitmap getBitmap(){return bmp;}
}
//** populate your array with each item in list
ArrayList<Post> array = new ArrayList<Post>();
Post p = new Post();
//* ..............
ListAdapter adapter = new ListAdapter (this, array);
listView.setAdapter(adapter);
private class ListAdapter extends BaseAdapter
{
Context context;
ArrayList<Post> contactList;
public ListAdapter(Context context, ArrayList<Post> contactList)
{
this.context = context;
this.contactList = contactList;
}
public View getView(int position, View convertView, ViewGroup parent)
{
try
{
if(convertView==null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
TextView timeview = (TextView) convertView.findViewById(R.id.time);
TextView msgview = (TextView) convertView.findViewById(R.id.message);
Post post = getItem(position);
imageView.setImageBitmap(post.getBitmap());
timeView.setText(post.getTime());
messageView.setText(post.getMessage());
int width = convertView.getWidth();
int height = convertView.getHeight();
int messageWidth = post.getMessage().length();
//* adjust the width based on your message width
//* width = ........ do something here
//* set the desired width here
convertView.setLayoutParams(new LayoutParams(width, height));
return convertView;
}
catch(Exception e)
{
}
return null;
}
#Override
public int getCount()
{
return contactList.size();
}
#Override
public Post getItem(int position)
{
return contactList.get(position);
}
#Override
public long getItemId(int position)
{
return 0;
}
}
Change code in list_item.xml
android:layout_width="wrap_content" to fill_parent or match_parent
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 make a file picker with a AlertDialog based on a ListView.
My problem is that my onClickListener seems to do nothing. So when I click on a line in my list nothing happens. Here is my FilePicker class :
public class FilePicker extends AlertDialog.Builder {
public FilePicker(final Context context) {
super(context);
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ListView v = (ListView) li.inflate(R.layout.file_list, null, false);
File[] dirList = (new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/myDir")).listFiles();
ArrayList<HashMap<String, String>> mylistData =
new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[] {"col1", "col2"};
for (File file: dirList){
HashMap<String,String> map = new HashMap<String, String>();
map.put(columnTags[0], file.getName());
map.put(columnTags[1], DateFormat.format("yyyy-MM-dd",new Date(file.lastModified())).toString());
mylistData.add(map);
}
int[] columnIds = new int[] {R.id.filelistitemview,R.id.datelistitemview};
SimpleAdapter adapter = new SimpleAdapter(context, mylistData,R.layout.file_list_item,columnTags , columnIds);
this.setAdapter(adapter, null);
this.setTitle("Choose midi settings file");
v.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList =(String) (v.getItemAtPosition(myItemInt));
Toast.makeText(context, selectedFromList, Toast.LENGTH_SHORT).show();
}
});
this.setView(v);
}
}
and here is my two xml files :
file_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and file_list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/filelistitemview"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#000"
android:textSize="23dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/datelistitemview"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#000"
android:textSize="18dp"
android:layout_weight="1"/>
</LinearLayout>
In my activity I just have to call my FilePicker like this :
FilePicker fp = new FilePicker(this);
fp.show();
instead of setting adapter to dialog object
this.setAdapter(adapter, null);
you probably need set it to your list view
v.setAdapter(adapter);
change final ListView v = (ListView) li.inflate(R.layout.file_list, null, false); line by
`View vi = inflater.inflate(R.layout.file_list, null`);
because here you inflate layout in list view and now
final ListView v = (ListView)vi.findViewById(your listviewid);
I created an Adapter to populate my custom listView and when ran on the emulator the activity is blank. Plz help. I am sure I'm missing something 'cause I am new to java & Android. Some code snippets to correct it and pointers will be appreciated. Thnx!
My Activity:
public class List_AC3 extends ListActivity {
/**
* -- Called when the activity is first created
* ===================================================================
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
displayResultList();
}
private void displayResultList() {
Cursor databaseCursor = null;
DomainAdapter databaseListAdapter = new DomainAdapter(this, R.layout.list_item, databaseCursor,
new String[] {"label", "title", "description"},
new int[] { R.id.label, R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
setListAdapter(databaseListAdapter);
}
}
My Adapter:
public class DomainAdapter extends SimpleCursorAdapter{
private LayoutInflater mInflater;
String extStorageDirectory;
public DomainAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.label);
holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
holder.text3 = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File dbfile = new File(extStorageDirectory+ "/Aero-Technologies/flyDroid/dB/flyDroid.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Cursor data = db.rawQuery("SELECT * FROM AC_list", null);
data.moveToPosition(position);
int label_index = data.getColumnIndex("label");
String label = data.getString(label_index);
int title_index = data.getColumnIndex("title");
String title = data.getString(title_index);
int description_index = data.getColumnIndex("description");
String description = data.getString(description_index);
holder.text1.setText(label);
holder.text2.setText(title);
holder.text3.setText(description);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
The list_view2.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="30dip"
android:padding="4dip"
android:background="#drawable/gradient" >
<ImageButton
android:id="#+id/homeBtn"
android:src="#drawable/ic_menu_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#null" />
<TextView
android:id="#+id/titleBarTitle"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18sp" />
<ImageButton
android:id="#+id/toolBtn"
android:src="#drawable/ic_menu_list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="#null" />
</RelativeLayout>
<ListView
android:id="#id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
And my list_item.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/acItem"
style="#style/listItem" >
<TextView
android:id="#+id/label"
style="#style/listAcronym" />
<TextView
android:id="#+id/listTitle"
style="#style/listTitle" />
<TextView
android:id="#+id/caption"
style="#style/listDiscription"/>
<ImageView
style="#style/listNextIcon" />
</RelativeLayout>
the google notepad tutorials should also help you IIRC they should be using cursors passed to a listview
SimpleCursorAdapter doesn't need to be extended to work. Take your db logic out of getView and use it to construct a cursor which actually points to a db result. Then pass that cursor to the SimpleCursorAdapter constructor. In fact, I don't think getView is actually being called anywhere.
Try getting this example working http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/ first and then edit it to do what you need.
If you want to do something more complex (like setting the various textviews yourself like you're trying to do in getView) look into CursorAdapter.
The correct answer can be found HERE along with the code.