Get row position in ListView with Checkbox in Android - android

I am writing an android app, and I am having trouble getting a row's position in ListView with CheckBox.
Here is a screenshot on the emulator:
I already made the ListView with CheckBoxes. But I can't figure out how find the row's position for each checked entry. And once I get the checked positions, I want to implement a Remove Selected action.
Please help!
Here is my code:
public class ListTest extends ListActivity implements OnCheckedChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//--- Importing StringsArray---
Bundle gotBasketScanner = getIntent().getExtras();
String[] StringArrayHistory = gotBasketScanner.getStringArray("fromHistory");
// ---Make Adapter---
setListAdapter( new ArrayAdapter<String>(ListTest.this,
android.R.layout.simple_list_item_multiple_choice,
StringArrayHistory));
// ---Put Array into a ListView and add Checkboxes to Listview
ListView list = getListView();
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}

hey check this it will help you...
http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php
Android ListView with Checkbox and all clickable
http://appfulcrum.com/2010/09/12/listview-example-3-simple-multiple-selection-checkboxes/

Use onItemClick() in instead of onCheckedChangeListener...
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getApplicationContext(), "Item "+position+" is clicked",
Toast.LENGTH_SHORT).show();
}
}
This is the better way to get the clicked position and id.
I hope this will solve your problem.
EDIT
I have updated the following line:-
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
now try ur code.

You can use PrefrenceScreen with CheckBoxPreference for easier and scalellable implementation

You can use CheckedTextView to implement the same:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple">
</CheckedTextView>
addlist.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" >
<ListView
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
<Button android:id="#+id/click"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click"/>
</LinearLayout>
And here is the activity GetCheckedTextPositionActivity. On click of the button you will get the positions of the listitem checked :
public class GetCheckedTextPositionActivity extends Activity {
private ListView myList;
private EfficientAdapter myAdapter;
private ArrayList<String> data;
private LayoutInflater mInflater;
private Button click;
private ArrayList<Integer> positions;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addlist);
positions = new ArrayList<Integer>();
data = new ArrayList<String>();
data.add("Bangalore");
data.add("Delhi");
data.add("Patna");
data.add("Mumbai");
click = (Button)findViewById(R.id.click);
mInflater = LayoutInflater.from(getApplicationContext());
myList = (ListView) findViewById(R.id.mainListView);
myAdapter = new EfficientAdapter(data, getApplicationContext());
myList.setAdapter(myAdapter);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(positions.size() != 0){
for(Integer pos : positions){
Log.e("Positions checked :"," Positions :"+pos);
}
}
}
});
}
public class EfficientAdapter extends BaseAdapter {
private ArrayList<String> myData;
public EfficientAdapter(ArrayList<String> mData, Context ctx) {
// TODO Auto-generated constructor stub
this.myData = mData;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myData.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myData.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int currentPos = position;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.main, null);
}
((CheckedTextView) convertView).setText(myData.get(position));
((CheckedTextView) convertView).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((CheckedTextView) v).toggle();
if(((CheckedTextView) v).isChecked()){
positions.add(currentPos);
}
}
});
return convertView;
}
}
}
Hope this helps.

Related

How to display checkbox on every entry in a listView

From this Activity i get text from textField and display it in a ListView.
Now i want to to add check box on every entry in a listView Cell and also like to know how to display more than one text in a single ListView Cell.
Help with code will be appreciated.
Here is my code ....
public class AfterRegister extends AppCompatActivity
{
ListView listView;
EditText editText;
Button insertItemButton;
ArrayList<String> arrayList = new ArrayList<String>();
ArrayAdapter<String> adapter;
CheckBox checkBox;
StoreRegistrationDataBase storeRegistrationDataBase;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_after_register);
storeRegistrationDataBase = new StoreRegistrationDataBase(this);
storeRegistrationDataBase = storeRegistrationDataBase.open();
checkBox = (CheckBox) findViewById(R.id.checkbox);
insertItemButton = (Button) findViewById(R.id.button4);
insertItemButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
editText = (EditText) findViewById(R.id.editText2);
listView = (ListView) findViewById(R.id.listView);
String getEditTextString = editText.getText().toString();
if(isAlphaNumeric(getEditTextString))
{
if(!getEditTextString.equals(""))
{
arrayList.add(getEditTextString);
adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.text_view_layout, R.id.achView1, arrayList);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
editText.setText("");
}
else
{
Toast.makeText(AfterRegister.this, "You can not insert empty field", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(AfterRegister.this, "Remove Space", Toast.LENGTH_SHORT).show();
}
}
});
listView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
return false;
}
});
}
public boolean isAlphaNumeric(String s)
{
String pattern= "^[a-zA-Z0-9]*$";
if(s.matches(pattern))
{
return true;
}
return false;
}
}
You have to use a BaseAdapter and some Getter/Setter methods to add multiple texts/images/other UI elements in each item of your list view.
You have to implement multiple things to get this result. Here they are --
Create a Custom Layout for each item of your ListView.
listview_item_layout.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/layout_textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginRight="5dip"
android:textStyle="bold"/>
<TextView
android:id="#+id/layout_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_marginLeft="5dip"
android:textStyle="bold"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"
android:text="Test"/>
</LinearLayout>
Create a custom class and add some Getter/Setter methods.
ListRowItem.java
public class ListRowItem implements Serializable{
String carrier,number;
public String getCarrier(){
return carrier;
}
public String getNumber(){
return number;
}
public void setCarrier(String ba_carrier){
carrier = ba_carrier;
}
public void setNumber(String ba_number){
number = ba_number;
}
}
Create a custom class and extend the BaseAdapter class.
public class MyBaseAdapter extends BaseAdapter {
public Context ba_context;
public ArrayList<ListRowItem> listitem = new ArrayList<>();
public LayoutInflater inflater;
ListRowItem currentlistitem;
public MyBaseAdapter(Context ma_context, ArrayList<ListRowItem> ma_listitem) {
super();
this.ba_context = ma_context;
this.listitem = ma_listitem;
inflater = (LayoutInflater) ba_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return this.listitem.size();
}
#Override
public Object getItem(int position) {
return this.listitem.get(position);
}
#Override
public long getItemId(int position) {
return (long) position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.listview_item_layout, parent, false);
TextView carrier = (TextView) vi.findViewById(R.id.layout_textview1);
TextView number = (TextView) vi.findViewById(R.id.layout_textview2);
currentlistitem = listitem.get(position);
String str_carrier = currentlistitem.getCarrier();
String str_number = currentlistitem.getNumber();
carrier.setText(str_carrier);
number.setText(str_number);
return vi;
}
}
Finally, populate your ArrayList and set the Adapter in your MainActivity.
ArrayList<ListRowItem> listitem = new ArrayList<>();
Context context = TestActivity.this;
MyBaseAdapter baseAdapter;
ListRowItem lr = new ListRowItem();
lr.setNumber(number);
lr.setCarrier(carrier);
listitem.add(lr);
baseAdapter = new MyBaseAdapter(context,listitem);
setContentView(R.layout.activity_test);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(baseAdapter);
Hope this helps!!

How to update a ListView that use CursorAdapter when click a button in listview's row

I'm getting EditText's text when I click the "Save" button and this text is saved to Database and a cursor returns form Database then displayed in Listview. All saving and getting operations have no problem. When I click the save button, the list have been refreshed dynamically but When I click the "Delete" button in list row,altough the cursor updated the listview doesn't refresh itself.
I dont know what's wrong...
Thanks a lot for helps...
MainActivity'S oncreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DB = new DataBase(this,"CursorDatas",null,1);
ContextWrapper.setContext(getApplicationContext());
editText =(EditText) findViewById(R.id.edittext);
btnSave = (Button)findViewById(R.id.btn_save);
recordsList = (ListView)findViewById(R.id.list);
cursor = DB.getRecords();
listAdapter = new ListAdapter(this,cursor);
recordsList.setAdapter(listAdapter);
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = editText.getText().toString();
DB.addRecord(str);
cursor = DB.getRecords();
listAdapter = new ListAdapter(getApplicationContext(),cursor);
recordsList.setAdapter(listAdapter);
}
});
}
My ListAdapter class
public class ListAdapter extends CursorAdapter {
LayoutInflater inf;
ViewHolder vHolder;
DataBase DB;
Cursor cursor;
ListView lW;
ListeAdaptor listAdapter;
Context context;
public ListeAdaptor(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inf.inflate(R.layout.activity_main, null);
lW = (ListView)v.findViewById(R.id.list);
DB = new DataBase(context,"CursorDatas",null,1);
this.context = context;
}
#Override
public void bindView(View arg0, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
vHolder = (ViewHolder)arg0.getTag();
vHolder.tV.setText(arg2.getString(arg2.getColumnIndex("Name")));
vHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = vHolder.tV.getText().toString();
DB.deleteRecords(isim);
cursor = DB.getRecords();
listAdapter = new ListAdapter(ContextWrapper.getContext(),cursor);
cursor.requery();
listAdapter.notifyDataSetChanged();
lW.setAdapter(listAdapter);
}
});
}
#Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v = inf.inflate(R.layout.list_item,null);
ViewHolder viewHolder = new ViewHolder(v);
v.setTag(viewHolder);
return v;
}
}
List Item.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" >
<TextView
android:id="#+id/name"
android:layout_width="100dp"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/btn_delete"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_toRightOf="#id/name"
android:layout_toEndOf="#id/name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="Delete"></Button>
There is a best solution for this in which you would not have to bother updating and reassigning your adapter and setting it to your listview again and again.
You should use 'CursorLoader' along with CursorAdapter and it will automatically manage the updation to your listview.here is link.
http://developer.android.com/reference/android/content/CursorLoader.html

ListActivity CustomAdapter error, onListItemClick never works

Below you can see the code, i implemented a simple adapter for my listview. But i can not get in to onListItemClick. can anyone has suggestions ?
actually it displays the list normally but i am not able to get onitemclick events . thanks in advance.
public class MyListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Frame> results = WebOperations
.loadList_Frame();
myAdapter = new MyListAdapter(MyListActivity.this);
myAdapter.internalList = results;
setListAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
};
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
}
public static class MyListAdapter extends BaseAdapter {
public ArrayList<Frame> internalList;
public LayoutInflater mInflater;
public int pageCount = 0;
public MyListAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
if (internalList == null)
return 0;
return internalList.size();
}
public Object getItem(int position) {
if (internalList == null || internalList.size() < position)
return null;
return internalList.get(position);
}
public long getItemId(int position) {
if (internalList == null || internalList.size() < position)
return 0;
return internalList.get(position).getId();
}
public View getView(int position, View arg1, ViewGroup parent) {
View v = arg1;
if ((v == null) || (v.getTag() == null)) {
v = mInflater.inflate(R.layout.entryrow, null);
try {
String gunlukText = String.format(" %s ",
internalList.get(position).getKeyText().toString());
TextView entry = (TextView) v
.findViewById(R.id.textViewEntryText);
entry.setText((CharSequence) gunlukText);
} catch (Exception e) {
Log.d("aaaaaaaaaaaaaaaaaaa", "errorrrrrrrrrrr");
}
}
return v;
}
}
}
EDIT 1 : I am adding entry_row layout xml file below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewEntryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:padding="5dp"
android:paddingLeft="10dp"
android:text="#string/Ana_Sayfa"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
You should consider adding your listener to your listview :
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " please show toast!!!!!!!!!", Toast.LENGTH_LONG).show();
}
});
add onclicklistener into getView method just before return view.
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.w("position", position + "");
}
});
check if it helps..
Didi you try, on your layout/entryrow, to add the addStatesFromChildrenattribute and set it to true ?
http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:addStatesFromChildren
Please refer one of the good example given in API demo of custom BaseAdapter from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html Just override onListItemCLicked and check. It works perfectly fine. Try to modify your code accordingly.

List view Edit text row got cleared when moving from one row to another

I have an xml with one textveiw and one edittext in it .I used this xml to get different rows in the list view of another xml file to list the data in that page.
The xml file that is used to list data in listview is as given below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="0dip">
<TextView
android:id="#+id/tv_Sort_Address"
android:layout_width="0dip"
style="#style/ListTextViewStyle"
android:layout_height="wrap_content"
android:text="No"
android:layout_weight="2"/>
<EditText
android:textColor="#000000"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:inputType="number"
style="#style/EditTextStyle"
android:background="#android:drawable/editbox_background"
android:id="#+id/et_Sort_Order"
android:layout_weight="1">
</EditText>
</LinearLayout>
I filled the Listview with data on another xml fils as given below
simpleAdapter = new SimpleAdapter(this, alist,
R.layout.sort_order_list_row, new String[] { "Sort_Address",
"Sort_FKDeliveryStatus", "Sort_PKDelivery",
"Sort_DeliveryOrder" }, new int[] {
R.id.tv_Sort_Address, R.id.tv_Sort_FKDeliveryStatus,
R.id.tv_Sort_PKDelivery, R.id.et_Sort_Order });
listSortOrder.setAdapter(simpleAdapter);
The problem with me is that if i have two row in the listview and when i
placed a value in one of edit text row and move on to the another rows edit text the value placed in the first row got disappered.
<ListView android:id="#+id/lv_Sort_Order"
style="#style/ListTextBackGround"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dip"
android:descendantFocusability="afterDescendants"
android:layout_marginRight="5dip" >
</ListView>
to solve this I placed android:descendantFocusability="afterDescendants" in the list view page to get focus on to the merged page. But it didn't solve the problem. Will anyone help me please.
Is there any property I need to be placed in the list view or in the merged Page?
Use this code :
public class ListViewDemo extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
private String[] usernameArr = null;
private String[] scoreArr = null;
//ArrayList<String> usernameArrLst = null ,scoreArrLst = null;
//private Helper helper = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewScore=(ListView)findViewById(R.id.listViewScore);
usernameArr = new String[]{"Alan","Bob","Carl","Doaniel","Evan","Fred","Gram"};
scoreArr = new String[usernameArr.length];
//usernameArrLst = new ArrayList<String>(Arrays.asList(usernameArr));
//scoreArrLst = new ArrayList<String>(Arrays.asList(scoreArr));
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArr==null){
return 0;
}
return usernameArr.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArr[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder ;
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(ListViewDemo.this);
rowView=layoutinflate.inflate(R.layout.listviewnewtext, parent, false);
viewHolder = new ViewHolder();
viewHolder.tv_Sort_Address = (TextView)rowView.findViewById(R.id.tv_Sort_Address);
viewHolder.et_Sort_Order = (EditText)rowView.findViewById(R.id.et_Sort_Order);
viewHolder.et_Sort_Order.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
scoreArr[viewHolder.ref] = s.toString();
}
});
rowView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.ref = position;
viewHolder.tv_Sort_Address.setText(usernameArr[position]);
viewHolder.et_Sort_Order.setText(scoreArr[position]);
return rowView;
}
}//class ends
class ViewHolder{
TextView tv_Sort_Address;
EditText et_Sort_Order;
int ref;
}
}
Is there any reason why you use SimpleAdapter? Can you try using ArrayAdapter or BaseAdapter.
Check this and any BaseAdapter example.

Constructing Dynamic List View In Android

Iam trying to create a list view dynamically, Please provide me an example for list view in android...
Activity class......
public class UsersListActivity extends ListActivity{
RegisterUser registerUser;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] statesList = getListOfAllUsers();
Toast.makeText(getApplicationContext(),
"States Array lentgh is : " + statesList.length,
Toast.LENGTH_LONG).show();
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
statesList));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"You selected : "+((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}}
and create an xml file named list_item.xml and paste the below code.
<?xml version="1.0" encoding="utf-8"?>
<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:textColor="#ffffff" android:textStyle="bold" android:background="#drawable/border_cell">
</TextView>
You can create your own List View by extending Base adapter class
public class ListAdapterDroidman extends BaseAdapter{
private ArrayList<ListitemDroidman> list = new ArrayList<ListitemDroidman>();
#SuppressWarnings("unused")
private Context context;
public ListAdapterDroidman(Context context) {
this.context = context;
}
public void addItem(ListitemDroidman item) {
list.add(item);
}
public void addItem(ListitemDroidman item,int pos)
{
list.add(pos, item);
}
public void removeItem(int pos)
{
list.remove(pos);
}
public void clearList()
{
list.clear();
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return list.get(position);
}
}
Then create your own layout
public class ListitemDroidman extends LinearLayout {
TextView filename;
ImageView iv;
public ListitemDroidman(Context context) {
super(context);
}
public void setInfo(Context context, Bitmap icon, String fname,int colorId) {
iv=new ImageView(context);
iv.setImageBitmap(icon);
iv.setPadding(0, 0, 20, 0);
addView(iv);
filename = new TextView(context);
filename.setTextSize(20);
filename.setTextColor(getResources().getColor(colorId));
filename.setText(fname);
addView(filename);
}
}
Now create listitems in your activityClass
create object of the adapter that u have created
myListitem = new ListitemDroidman(this);
icon = BitmapFactory.decodeResource(getResources(),
R.drawable.folder_icon);
myListitem.setInfo(getApplicationContext(), icon, filelist[i],
R.color.color_white);
add the listitem to the adaper as in normal list
la_file.addItem(myListitem);
Try this tutorial to understand how to use ListView.
http://developer.android.com/resources/tutorials/views/hello-listview.html
Why don't you just take a look at the ApiDemos of your android SDK..
You can find 14 examples of ListActivities implementations..
Or just search here or on Google, you can find lots of examples
Then if you want to achieve something in particular you can ask a more specific question here, does it sound fair?

Categories

Resources