hightlight multiple selected item(s) - android

i am populating my list view with cursor adapter, and i want to allow user to long press on any item(s) and perform available actions using action mode. i am using support library as my minimum sdk version =10
Problem: when i long click on the item action mode is displayed but the item is not highlighted as selected.
here is my activity_layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
and the layout for list row :
<?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:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:background="#drawable/activated_background"
>
<TextView
android:id="#+id/item_id"
android:layout_width="40dp"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
<TextView
android:id="#+id/item_amount"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
/>
</LinearLayout>
i have used android:background="#drawable/activated_background" for row layout and it is defined in drawable folder as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true" android:drawable="#drawable/list_activated_holo" />
<item android:drawable="#android:color/transparent" />
</selector>
when i long click on the item, it doesn't get highlighted.
here is my activity class:
public class MainActivity extends ActionBarActivity {
ActionMode _actionMode =null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.listView1);
SQLiteDatabase db = new ItemDbContract(getBaseContext()).getReadableDatabase();
String selection[] = {ProductTable._ID,ProductTable.TITLE,ProductTable.Price};
Cursor cursor = db.query(ProductTable.TABLE_NAME, selection, null, null, null, null, null);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(new ItemsListAdapter(getBaseContext(), cursor));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(_actionMode!=null)
{
listView.setItemChecked(arg2, true);
System.out.println("item position checked="+arg2);
}
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("long clicked on "+arg2);
listView.setItemChecked(arg2, true);
((ActionBarActivity)MainActivity.this).startSupportActionMode(actionModeCallback);
return true;
}
});
}
ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onDestroyActionMode(ActionMode arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateActionMode(ActionMode arg0, Menu menu) {
// TODO Auto-generated method stub
_actionMode = arg0;
_actionMode.getMenuInflater().inflate(R.menu.item_journal_context_menu, menu);
return true;
}
#Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
// TODO Auto-generated method stub
return false;
}
};
}
thanks in advance

finally solved my problem by maintaining a list of selected item in custom adapter and setting different background for the selected item.

Related

Integrating Listview and search

Im having hard time integrating search in a custom listview. I have a listview using layout.xml and adapter then when I added search in my existing listview the search is working but the layout in not show in the list.Kindly click here for the file
Android Zip Click here.
Main Activity
public class MainActivity extends Activity{
EditText search;
ListView listview;
ArrayList<Person> list=new ArrayList<Person>();
ArrayList<Person> source=new ArrayList<Person>();
ArrayAdapter<Person> adapter;
AlertDialog.Builder builder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//
this.search=(EditText) this.findViewById(R.id.editText1);
this.listview=(ListView) this.findViewById(R.id.listView1);
//populate source
//list.add(new Person(R.drawable.melvin,"Melvin ","PhD Educational Management"));
source.add(new Person(R.drawable.dennis,"Dennis ","Master in Information Technology"));
source.add(new Person(R.drawable.jonathan,"Jonathan ","PhD in Technological Management"));
source.add(new Person(R.drawable.bell,"Bell ","MS Information Technology"));
source.add(new Person(R.drawable.jennifer,"Jennifer ","PhD Technological Management"));
source.add(new Person(R.drawable.gian,"Gian","MS Information Technology"));
//delegatethe list to the adapter
adapter=new ArrayAdapter<Person>(this,android.R.layout.simple_list_item_1,list);
//delegate the adapter to the listview
this.listview.setAdapter(adapter);
//delegate a key watcher
this.search.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
//clear the listview
list.clear();
//get match
String regex=arg0.toString();
Pattern p=Pattern.compile(regex);
for(int i=0;i<source.size();i++){
Matcher m=p.matcher((CharSequence) list.get(i));
while(m.find()){
list.add(source.get(i));
adapter.notifyDataSetChanged();
}
}
}
});
}
Adapter
package com.example.searchlist;
public class ItemAdapter extends BaseAdapter {
Context context;
ArrayList<Person> list;
LayoutInflater inflater;
public ItemAdapter(Context context, ArrayList<Person> list) {
super();
this.context = context;
this.list = list;
this.inflater=LayoutInflater.from(context);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
PersonHandler handler =null;
if(arg1==null){
arg1=inflater.inflate(R.layout.item_layout, null);
handler=new PersonHandler();
handler.iv=(ImageView) arg1.findViewById(R.id.imageView1);
handler.name=(TextView) arg1.findViewById(R.id.textView1);
handler.course=(TextView) arg1.findViewById(R.id.textView2);
arg1.setTag(handler);
}
else handler=(PersonHandler) arg1.getTag();
handler.iv.setImageResource(list.get(arg0).getImg());
handler.name.setText(list.get(arg0).getName());
handler.course.setText(list.get(arg0).getCourse());
return arg1;
}
static class PersonHandler{
ImageView iv;
TextView name, course;
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0000ff" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#ff0000" />
</LinearLayout>
Activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4682b4"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:ems="10"
android:hint="SEARCH"
android:padding="10dp" >
<requestFocus />
</EditText>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fafad2" >
</ListView>
</LinearLayout>

highlighting selected item in custom list view using context menu?

I've been trying to set my background colour on this list view and with following multiple tutorials it just doesn't come up, yet no errors are shown.
What I've got is an xml file called colours in the values folder:
<resources>
<drawable name="red_colour">#6D0E0E</drawable>
</resources>
Which links to an xml in the drawable folder:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_activated="true"
android:drawable="#drawable/red_colour">
</item>
</selector>
I've tried placing the following code android:background:#id"colourhighlight.xml"
literally everywhere based on various tutorials yet no luck?
Any advice? Please help...
I've got one main class with a context menu:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_contacts);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[] { "name","photo","details"},
new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
// Getting reference to listview
final ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
//Selecting and highlighting the elements in the listview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setActivated(true);
}
});
//Creating the context menu and the options for it
listview = (ListView) findViewById(R.id.lst_contacts);
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listview.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
count = count+1;
mode.setTitle(count + " Contacts Selected");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contact_context_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.delete_id:
Toast.makeText(getBaseContext(), count + " Contacts Deselected", Toast.LENGTH_SHORT).show();
count = 0;
mode.finish();
return true;
//break;
default:
return false; //break;
}
//return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
});
}
lv_layout.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"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/iv_photo"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tv_name"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/tv_details"
android:layout_below="#+id/tv_name"
android:layout_toRightOf="#+id/iv_photo"
android:layout_toEndOf="#+id/iv_photo" />
</RelativeLayout>
Main layout.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="com.example.ankhit.saveme.UserContacts"
android:background="#drawable/colourhighlight"
>
<ListView
android:id="#+id/lst_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/instructions"
android:id="#+id/button"
android:onClick="showInstructions"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
{
try
{
final int checkedCount = listView.getCheckedItemCount();
mode.setTitle(""+checkedCount);
if (checked)
{
adapter.setNewSelection(position, checked);
}
else
{
adapter.removeSelection(position);
}
adapter.notifyDataSetChanged();
}
catch (Exception e)
{
e.printStackTrace();
}
}
add these methods in your adapter class
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
public void setNewSelection(int position, boolean value)
{
mSelection.put(position, value);
}
public boolean isPositionChecked(int position)
{
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
}
call this method inside getView() method of adapter it works properly
convertView.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
if (mSelection.get(position) != null)
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.message_selector_holo_blue));
}
If you want highlighting selected row completely then change the background color of the convertview
lstContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
}
});

Android: Drop Down Menu - who have simple example?

Who have simple example for Drop Down Menu by Button Click?
Need make list for installed programs and select for starting.
Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.popup_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.food:
makeToast("food","","","");
return true;
case R.id.other:
makeToast("other","","","");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And XML file of popup_menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/food" android:title="Food" />
<item android:id="#+id/other" android:title="Other" />
</menu>
This example is create myself to when you select any item in dropdown (spinner) list at a moment image display rightside on base on select item. so this example help to you.
MainActivity.java
public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] technology = {"PHP", "Ruby", "Java", "SQL"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
spinnertech.setAdapter(adapter);
spinnertech.setOnItemSelectedListener(this);
// Spinner Start....
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position = arg0.getSelectedItemPosition();
ImageView ivtech = (ImageView) findViewById(R.id.imgtech);
if(position == 0) {
ivtech.setImageResource(R.drawable.php);
} else if(position == 1) {
ivtech.setImageResource(R.drawable.ruby);
} else if(position == 2) {
ivtech.setImageResource(R.drawable.java);
} else if(position == 3) {
ivtech.setImageResource(R.drawable.sql);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// Spinner End....
}
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/TableLayout1"
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"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imgtech"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_marginRight="10dp" />
<Spinner
android:id="#+id/spinnertech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1" />
</LinearLayout>
</TableLayout>

Adding search feature to listview

I have created a listview of the users installed applications in a sliding drawer and I want to add a search function to it. I am following the tutorial here.
So far, this is my coding (that includes the so far implemented search function):
Drag_and_Drop_App.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" >
<SlidingDrawer
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:layout_marginTop="200dp"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00FF00">
<!-- Editext for Search -->
<EditText android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search applications.."
android:inputType="textVisiblePassword"/>
<ListView
android:id="#+id/lvApps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Drag_and_Drop_App.java:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class Drag_and_Drop_App extends Activity {
private ListView mListAppInfo;
// Search EditText
EditText inputSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout for the main screen
setContentView(R.layout.drag_and_drop_app);
// search bar
inputSearch = (EditText) findViewById(R.id.inputSearch);
// load list application
mListAppInfo = (ListView)findViewById(R.id.lvApps);
// create new adapter
AppInfoAdapter adapter = new AppInfoAdapter(this, Utilities.getInstalledApplication(this), getPackageManager());
// set adapter to list view
mListAppInfo.setAdapter(adapter);
// implement event when an item on list view is selected
mListAppInfo.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int pos, long id) {
// get the list adapter
AppInfoAdapter appInfoAdapter = (AppInfoAdapter)parent.getAdapter();
// get selected item on the list
ApplicationInfo appInfo = (ApplicationInfo)appInfoAdapter.getItem(pos);
// launch the selected application
Utilities.launchApp(parent.getContext(), getPackageManager(), appInfo.packageName);
}
});
}
}
I'm not sure where to put this coding:
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
If you need to see any other of my coding (such as any adapters for the app listview) let me know!

Customlistview not inflating

I was trying to implement simple customlistview.What i tried to do is to inflate a text and a picture inside each list row in the listview.I got no output i.e. layout was not inflating..I got a blank screen.I used 2 Java class and 2 xml files.Below is my code..Please let me know where is my mistake and what should i correct?
CListView.java
package com.example.customlistview;
public class Clistview extends Activity {
int p1[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] Shapes1={"circle","circle","circle","circle","circle"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clistview);
lv=(ListView)findViewById(R.id.listView1);
CustomListViewAdapter clvadapter=new CustomListViewAdapter(Clistview.this,Shapes1,p1);
lv.setAdapter(clvadapter);
}
}
CustomListViewAdapter.java
package com.example.customlistview;
public class CustomListViewAdapter extends BaseAdapter {
Context context;
String shape1[];
int pic[];
public CustomListViewAdapter(Context c,String[] sh,int[] p)
{
this.context=c;
this.shape1=sh;
this.pic=p;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public String getItem(int pos) {
// TODO Auto-generated method stub
return shape1[pos];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View v, ViewGroup vg) {
// TODO Auto-generated method stub
View itemview=v;
TextView tv1;
ImageView im1;
if(itemview==null)
{
LayoutInflater inflater=(LayoutInflater)((Activity)context).getLayoutInflater();
itemview=inflater.inflate(R.layout.clistview1,vg,false);
}
tv1=(TextView)itemview.findViewById(R.id.textView1);
im1=(ImageView)itemview.findViewById(R.id.imageView1);
tv1.setText(shape1[pos]);
im1.setImageResource(pic[pos]);
return itemview;
}
}
activity_clistview.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Clistview" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp" >
</ListView>
</RelativeLayout>
clistview1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
/>
</LinearLayout>
Inside CustomListViewAdapter, change this
public int getCount() {
// TODO Auto-generated method stub
return shape1.length;
}
It is an important function that decides the number of items in a ListView.
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
You are returning 0 .
Return pic.length or shape1.length as per your requirement.
public int getCount() {
return pic.length;
}
Method getCount() returns the number of items in the datatset for the adapter.
getCount()

Categories

Resources