Display multiple fields from POJO object in listview - java

I have a list of a POJO in an Android app and I am currently displaying one of the fields in a listview/listitem like so:
List<NotificationItem> notifItems;
// snip, populate
ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.notification_item, notifItems);
ListView listView = (ListView) findViewById(R.id.notification_listview);
listView.setAdapter(adapter);
And it is my understanding that the listivew or adapter uses the toString of the POJO, which is
public String toString() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
return _notificationTitle + " | " + dateFormatter.format(_notificationReceivedDate);
}
R.id.notificationitem is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold"></TextView>
So far so good, but what I want to do is add elements to the notificationitem layout file and then update the Java code to populate the new fields.
How is this done? I don't really understand how the adapter knows/puts the toString value into the one field in the notificationitem.

public class NotificationItem {
//your fields here
}
//-----------------------
public class NotifAdapter extends BaseAdapter {
Context context;
List<NotificationItem> data;
public NotifAdapter(Context context, List<NotificationItem> data) {
this.context = context;
this.data = data;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.your_row_layout,parent, null);
TextView tx = convertView.findViewById(R.id.your_widget_inside_row_layout);
tx.setText(data.yourDataPropery);
return convertView;
}
}
// In your activity
NotifAdapter adapter = new NotifAdapter(this, ArrayList<NotificationItem>);
listView.setAdapter(adapter);

Related

Adding a second textview to customAdapter ListView

So I have a listview with one textview and I want to add a second textview to it. But I cannot figure out how to modify the adapter and then call it in my main activity, even after looking through many similiar questions on stackoverflow.
Heres how my CustomAdapter.java is now
class CustomAdapter extends ArrayAdapter<CharSequence>{
public CustomAdapter(Context context, CharSequence[] routes) {
super(context, R.layout.custom_row ,routes);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater routeInflater = LayoutInflater.from(getContext());
View customView = convertView;
if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}
CharSequence singleRoute = getItem(position);
TextView routeText = (TextView) customView.findViewById(R.id.routeText);
routeText.setText(singleRoute);
///// Textview I want to add
CharSequence routeNum = getItem(position);
TextView routeNumText = (TextView) customView.findViewById(R.id.numbersTextView);
routeNumText.setText(routeNum);
/////
return customView;
and heres my MainActivity.java
///// fill listview numbers I want to add
final String[] routeListviewNumbers = getResources().getStringArray(R.array.routeNumbers);
//fill list view with xml array of routes
final String[] routeListViewItems = getResources().getStringArray(R.array.routeList);
//custom adapter for list view
ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems);
final ListView routeListView = (ListView) findViewById(R.id.routeListView);
routeListView.setAdapter(routeAdapter);
any help in how to modify the adapter and then call it in the main activity would be really appreciated. Thank you!
I would recommend creating a custom Route class since this adapter is meant to handle one array. Create some member variables for the route number and route items in the new class with getter and setter methods. Then you should be able to create a new array list of Route objects in your main activity and iterate through the existing string arrays while appending them (as new Route objects) to the new array.
You will have to change the adapter to accept a Route object instead of CharSequence. Hope this points you in the right direction.
I would recommend a custom adapter for you.
Exm..
Create Route Class
class Route{
public int number;
public String text;
}
Base Adapter...
public class myBaseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Route> mRouteList;
public BilgiAdapter(Activity activity,List<Route> routeList){
mInflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRouteList = routeList;
}
#Override
public int getCount() {
return mRouteList.size();
}
#Override
public Object getItem(int position) {
return mRouteList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView;
myView = mInflater.inflate(R.layout.line_layout,null);
Route r = mRouteList.get(position);
TextView txtRouteNumber = (TextView)myView.findViewById(R.id.textRouteNumber);
TextView txtRouteText = (TextView)myView.findViewById(R.id.textRouteText);
txtRouteNumber.setText(String.ValueOf(r.number));
txtRouteText.setText(String.ValueOf(r.text));
return myView;
}
}
MainActivity vs..
ListView lstRoute;
myBaseAdapter adapter;
List<Route> list;
...
..
..
..
..
OnCreate(..)
..
list = new ArrayList<Route>();
//add routes in list
myBaseAdapter = new myBaseAdapter(MainActivity.this,list);
lstRoute = (ListView)findViewById(R.id.listViewRoute);
lstRoute.setAdapter(myBaseAdapter);
...
..
..
line_layout.xml(Layout file)
<?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="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textRouteNumber"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textRouteText"/>
</LinearLayout>

Android Studio Dynamically Changing Images in GridView

I have set up a GridView Successfully, however I want to be able to change the Images within the the GridView dependent upon a variable. For example:
int a = 2
if (a == 2){
Integer[] images = {
R.drawable.image1
R.drawable.image3
}
} else {
Integer[] images = (
R.drawable.image2
R.drawable.image4
}
}
However I can't find a way to do this. The code I am working with at the moment is:
GridView cardList = (GridView) findViewById(R.id.cardList);
cardList.setAdapter(new ImageAdapter(this));
cardList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(Create.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(215, 215));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(images[position]);
return imageView;
}
private Integer[] images = {
R.drawable.iamge1, R.drawable.image2
};
}
Try something like this:
The activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridView);
int images[] = {R.drawable.ic_action_name};
ImageAdapter imageAdapter = new ImageAdapter(getApplicationContext(),images);
gridView.setAdapter(imageAdapter);
}
}
Your adapter class:
public class ImageAdapter extends BaseAdapter {
private int images[];
Context context;
public ImageAdapter(Context context, int images[]){
this.context = context;
this.images = images;
}
private class Holder{
ImageView imageView;
}
#Override
public int getCount(){
return images.length;
}
#Override public Object getItem(int position){
return null;
}
#Override
public long getItemId(int position){
return 0;
}
#Override
public View getView(final int position, View view, ViewGroup parent){
//All of this Holder stuff is so that the View doesn't jump around wildly
final Holder holder;
LayoutInflater inflater;
if (view == null){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.singleimage,null);
holder = new Holder();
holder.imageView = (ImageView) view.findViewById(R.id.singleImage);
view.setTag(holder);
}
else {
holder = (Holder) view.getTag();}
holder.imageView.setImageResource(images[position]);
return view;
}
}
You also need a simple xml file for the single image in the gridView. Name this "singleimage.xml"
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/singleImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Activity xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.ri.emily.testapp.MainActivity">
<GridView
android:layout_width="match_parent"
android:id="#+id/gridView"
android:layout_height="match_parent" />
</RelativeLayout>

Printing SQLite entries into a ListView

I am having some trouble learning the ins and outs of the SQLite world. I have some code that is allowing me to enter data into a DB. But what i want to do is return this data into a listview. At the moment all I could figure out to do was to have each row printed in a toast after a new entry is added. Can someone please show me how to alter my code to print it in a listview? Or to even look at my code and see that i am going about it in the right way. Thanks
This is the code i am using which calls a display record function
//---get all Records---
com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do {
DisplayRecord(c);
} while (c.moveToNext());
}
db.close();
This is the display record function
public void DisplayRecord(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"Item: " + c.getString(1) + "\n" +
"Litres: " + c.getString(2),
Toast.LENGTH_SHORT).show();
}
I know i need to change the second function but i dont know how to do that to make it print into a listview
this is the code of getting data from database and insert into Arraylist and insert into arrayAdapter and than display it in listview .
i just done some editing in your existing code.
com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
db.open();
ArrayList<String> data_list=new ArrayList<String>();
ListView lv=(ListView)findViewById(R.id.listView1);
Cursor c = db.getAllRecords();
if (c.moveToFirst())
{
do {
data_list.add(c.getString(0));
DisplayRecord(c);
} while (c.moveToNext());
}
ArrayAdapter<String> aa=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, data_list);
lv.setAdapter(aa);
lv - is the object of ListView.
Create a ListView. Then provide cursorAdapter to the ListView as it's adapter to bind the data in the database to the ListView.
There are examples in the samples folder of the SDK you downloaded in the project called ApiDemos.
You need to have, 1) listview, 2) Object class, 3) Custom Adapter
Here I have just tried to implement as per your requirement.
Since I dont have db I did not try to run. The point to is to five you idea.
Because listview is widget that we use more frequently in android. This is the best approach as per my knowledge.
Layouts that required,
activity_list.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=".ListActivity" >
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
row_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="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/Item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item" />
<TextView
android:id="#+id/Litres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="litres" />
</LinearLayout>
ListActivity.java :
public class ListActivity extends Activity {
ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();
ListView list;
ListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
list = (ListView) findViewById(R.id.list);
listAdapter = new ListAdapter(ListActivity.this, rowDataArrayList);
list.setAdapter(listAdapter);
getDataFromDB();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
private void getDataFromDB() {
rowDataArrayList.clear();
com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(
this);
db.open();
Cursor c = db.getAllRecords();
if (c.moveToFirst()) {
do {
/**
* Set your data in class
*/
RowData rowData = new RowData();
rowData.setId(c.getString(0));
rowData.setItem(c.getString(1));
rowData.setLitres(c.getString(2));
rowDataArrayList.add(rowData);
} while (c.moveToNext());
}
db.close();
/**
* To reflect new data set change in listview
*/
listAdapter.notifyDataSetChanged();
}
}
RowData.java : Model[pojo] class to save data and to bind in custom adapter.
public class RowData {
String Id;
String Item;
String Litres;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getItem() {
return Item;
}
public void setItem(String item) {
Item = item;
}
public String getLitres() {
return Litres;
}
public void setLitres(String litres) {
this.Litres = litres;
}
}
ListAdapter.java : custom adapter to bind in listview
public class ListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();
public ListAdapter(Context context, ArrayList<RowData> rowData) {
mContext = context;
rowDataArrayList = rowData;
}
#Override
public int getCount() {
return rowDataArrayList == null ? 0 : rowDataArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
Holder holder = new Holder();
view = View.inflate(mContext, R.layout.row_item, null);
holder.tvtItem = (TextView) view.findViewById(R.id.Item);
holder.tvtLitres = (TextView) view.findViewById(R.id.Litres);
view.setTag(holder);
} else {
view = convertView;
}
Holder holder = (Holder) view.getTag();
holder.tvtItem.setText(rowDataArrayList.get(position).getItem());
holder.tvtLitres.setText(rowDataArrayList.get(position).getLitres());
return view;
}
class Holder {
TextView tvtItem;
TextView tvtLitres;
}
}
public class ListAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<RowData> rowDataArrayList = new ArrayList<RowData>();
public ListAdapter(Context context, ArrayList<RowData> rowData) {
mContext = context;
rowDataArrayList = rowData;
}
#Override
public int getCount() {
return rowDataArrayList == null ? 0 : rowDataArrayList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
Holder holder = new Holder();
view = View.inflate(mContext, R.layout.row_item, null);
holder.tvtItem = (TextView) view.findViewById(R.id.Item);
holder.tvtLitres = (TextView) view.findViewById(R.id.Litres);
view.setTag(holder);
} else {
view = convertView;
}
Holder holder = (Holder) view.getTag();
holder.tvtItem.setText(rowDataArrayList.get(position).getItem());
holder.tvtLitres.setText(rowDataArrayList.get(position).getLitres());
return view;
}
class Holder {
TextView tvtItem;
TextView tvtLitres;
}
}

How to store class into android listview?

This is a simple listview example
public class Test extends ListActivity {
ArrayList<String> txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txt=new ArrayList<String>();
txt.add("diaplay text 1");
txt.add("diaplay text 2");
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, txt));
}
}
But this can only store string
I want do something like this
<ul>
<li data-meta="hidden text 1">display text 1</li>
<li data-meta="hidden text 2">display text 2</li>
</ul>
Because I want store more information in each list item
So I want store this class into listview
public class Item
{
public String displayText;
public String meta;
}
How can I do this?
You have to implement a custom Adapter for that. First we have to fix your view model, you call it Item:
public class Item
{
public String displayText;
public String meta;
}
Those fields should be private with appropriate getters and setters and constructors. If you want to modify such Items in a List you also need to implement equals() and hashCode(). If you do all that your Item class should look something like this:
public class Item {
private String displayText;
private String meta;
public Item(String displayText, String meta) {
this.displayText = displayText;
this.meta = meta;
}
public Item() {
}
public String getDisplayText() {
return displayText;
}
public void setDisplayText(String displayText) {
this.displayText = displayText;
}
public String getMeta() {
return meta;
}
public void setMeta(String meta) {
this.meta = meta;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Item item = (Item) o;
if (displayText != null ? !displayText.equals(item.displayText) : item.displayText != null) return false;
return !(meta != null ? !meta.equals(item.meta) : item.meta != null);
}
#Override
public int hashCode() {
int result = displayText != null ? displayText.hashCode() : 0;
result = 31 * result + (meta != null ? meta.hashCode() : 0);
return result;
}
}
Now we need to create a layout for the Items in the ListView, for example something simple like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:clickable="true"
android:background="#drawable/list_item_background">
<TextView
style="#style/DefaultTextView"
android:id="#+id/tvDisplayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
style="#style/DefaultTextView"
android:id="#+id/tvMeta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/tvDisplayText"
android:layout_toStartOf="#id/tvDisplayText"
android:layout_centerVertical="true" />
</RelativeLayout>
In this layout we have two TextViews to display both the Strings from each Item. To increase ListView performance you should always implement the view holder pattern. For this purpose we create a ViewHolder class. Its purpose is to hold a reference to the relevant Views in each row of the ListView so we don't have to perform the expensive findViewById() as often:
public class ViewHolder {
public TextView tvDisplayText;
public TextView tvMeta;
}
Note that we don't need getters and setters or anything in this view holder. We will access the public fields directly.
Now we can implement our custom Adapter. This is actually pretty straight forward, I will comment the most important parts.
public class ExampleAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final List<Item> items;
private ExampleAdapter(Context context, List<Item> items) {
this.inflater = LayoutInflater.from(context);
this.items = items;
}
#Override
public int getCount() {
return this.items.size();
}
#Override
public Item getItem(int position) {
return this.items.get(position);
}
#Override
public long getItemId(int position) {
return this.items.get(position).hashCode();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Item item = getItem(position);
if(convertView == null) {
// If convertView is null we have to inflate a new layout
convertView = this.inflater.inflate(R.layout.example_list_item, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.tvDisplayText = (TextView) convertView.findViewById(R.id.tvDisplayText);
viewHolder.tvMeta = (TextView) convertView.findViewById(R.id.tvMeta);
// We set the view holder as tag of the convertView so we can access the view holder later on.
convertView.setTag(viewHolder);
}
// Retrieve the view holder from the convertView
final ViewHolder viewHolder = (ViewHolder) convertView.getTag();
// Bind the values to the views
viewHolder.tvDisplayText.setText(item.getDisplayText());
viewHolder.tvMeta.setText(item.getMeta());
return convertView;
}
}
And you would use this custom Adapter like this:
final List<Item> items = new ArrayList<Item>();
items.add(new Item("a", "b"));
items.add(new Item("c", "d"));
items.add(new Item("e", "f"));
items.add(new Item("g", "h"));
final ExampleAdapter adapter = new ExampleAdapter(context, items);
listView.setAdapter(adapter);
You have implementation BaseAdapter, ArrayApdater or somethings.
In your adapter you pass a list of custom objects.
Here a sample code:
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Item> items;
public CustomListAdapter(Activity activity, List<Item> items) {
this.activity = activity;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int location) {
return items.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.yourlayout, null);
Item item = items.get(position);
//Setter text
return convertView;
}
}
Tutorial:
BaseAdapter
Official Documentation
To store custom data in a ListView adapter, you must implement your own. Take a look at Custom BaseAdapter with ListView.
I don't know clearly understand your question. But i understand that. Hope for help.
strings.xml
<string-array name="li_items">
<item >Home</item>
<item >Find People</item>
<item >Photos</item>
<item >Communities</item>
<item >Pages</item>
</string-array>
In your ListActivity
public class Test extends ListActivity {
ArrayList<String> txt;
private String[] liItem;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
liItem= getResources().getStringArray(R.array.li_items);
txt=new ArrayList<String>();
txt.add(liItem[0]);
txt.add(liItem[1]);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, txt));
}
}

Listview selects mutliple items when clicked

I'm trying to make a task manager, and I only have one problem. I have a listview that gets inflated. All the elements in the listview are correct. The problem is that when I select an item, the listview will select another item away. I've heard listviews repopulate the list as it scrolls down to save memory. I think this may be some sort of problem. Here is a picture of the problem.
If i had more apps loaded, then it would continue to select multiple at once.
Here is the code of my adapter and activity and XML associated
public class TaskAdapter extends BaseAdapter{
private Context mContext;
private List<TaskInfo> mListAppInfo;
private PackageManager mPack;
public TaskAdapter(Context c, List<TaskInfo> list, PackageManager pack) {
mContext = c;
mListAppInfo = list;
mPack = pack;
}
#Override
public int getCount() {
return mListAppInfo.size();
}
#Override
public Object getItem(int position) {
return mListAppInfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TaskInfo entry = mListAppInfo.get(position);
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(mContext);
//System.out.println("Setting LayoutInflater in TaskAdapter " +mContext +" " +R.layout.taskinfo +" " +R.id.tmbox);
convertView = inflater.inflate(R.layout.taskinfo,null);
}
ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage);
ivIcon.setImageDrawable(entry.getIcon());
TextView tvName = (TextView)convertView.findViewById(R.id.tmbox);
tvName.setText(entry.getName());
convertView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
final CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox);
if(v.isSelected())
{
System.out.println("Listview not selected ");
//CK.get(arg2).setChecked(false);
checkBox.setChecked(false);
v.setSelected(false);
}
else
{
System.out.println("Listview selected ");
//CK.get(arg2).setChecked(true);
checkBox.setChecked(true);
v.setSelected(true);
}
}
});
return convertView;
public class TaskManager extends Activity implements Runnable
{
private ProgressDialog pd;
private TextView ram;
private String s;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.taskpage);
setTitleColor(Color.YELLOW);
Thread thread = new Thread(this);
thread.start();
}
#Override
public void run()
{
//System.out.println("In Taskmanager Run() Thread");
final PackageManager pm = getPackageManager();
final ListView box = (ListView) findViewById(R.id.cBoxSpace);
final List<TaskInfo> CK = populate(box, pm);
runOnUiThread(new Runnable()
{
#Override
public void run()
{
ram.setText(s);
box.setAdapter(new TaskAdapter(TaskManager.this, CK, pm));
//System.out.println("In Taskmanager runnable Run()");
endChecked(CK);
}
});
handler.sendEmptyMessage(0);
}
Taskinfo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/tmImage"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="centerCrop"
android:adjustViewBounds="false"
android:focusable="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tmbox"
android:lines="2"/>
</LinearLayout>
Taskpage.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/cBoxSpace"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:orientation="vertical"/>
<TextView
android:id="#+id/RAM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="#+id/endButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Selected Tasks" />
</LinearLayout>
Any ideas for what reason mutliple items are selected with a single click would be GREATLY appreciated. I've been messing around with different implementations and listeners and listadapters but to no avail.
I think the point is you only save checking state in the view(v.setSelected).
And you reuse these view, so its checkbox is always not change its state.
You can create a state array to save every checking state of every TaskInfo, and check this array when you create a view.
for example
// default is false
ArrayList<Boolean> checkingStates = new ArrayList<Boolean>(mListAppInfo.size());
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TaskInfo entry = mListAppInfo.get(position);
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.taskinfo,null);
}
ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage);
ivIcon.setImageDrawable(entry.getIcon());
TextView tvName = (TextView)convertView.findViewById(R.id.tmbox);
tvName.setText(entry.getName());
final CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox);
checkBox.setChecked(checkingStates.get(position));
convertView.setSelected(checkingStates.get(position));
convertView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if(v.isSelected())
{
System.out.println("Listview not selected ");
//CK.get(arg2).setChecked(false);
checkBox.setChecked(false);
v.setSelected(false);
checkingStates.get(position) = false;
}
else
{
System.out.println("Listview selected ");
//CK.get(arg2).setChecked(true);
checkBox.setChecked(true);
v.setSelected(true);
checkingStates.get(position) = true;
}
}
});
return convertView;
}
I'm not 100% sure what you are trying to do, but part of your problem might be related to the condition in your onClick method:
if(v.isSelected())
I think you want that to read
if(v.isChecked())
isSelected is inherited from View, and it means something different from isChecked
Also, the whether the CheckBox is checked or not is independent from your data model since it is a recycled view. Your CheckBox should be checked based on entry (I'm assuming your TextInfo class has an isChecked() method that returns a boolean:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
TaskInfo entry = mListAppInfo.get(position);
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(mContext);
//System.out.println("Setting LayoutInflater in TaskAdapter " +mContext +" " +R.layout.taskinfo +" " +R.id.tmbox);
convertView = inflater.inflate(R.layout.taskinfo,null);
}
ImageView ivIcon = (ImageView)convertView.findViewById(R.id.tmImage);
ivIcon.setImageDrawable(entry.getIcon());
TextView tvName = (TextView)convertView.findViewById(R.id.tmbox);
tvName.setText(entry.getName());
CheckBox checkBox = (CheckBox)v.findViewById(R.id.tmbox);
checkBox.setChecked(entry.isChecked());
}
I don't think you need the View.OnClickListener you are attaching to convertView. You should handle that in the OnItemClickListener attached to the ListView. Assuming your ListView is called listView and TaskInfo instances have setChecked and isChecked methods:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
entry = mListAppInfo.get(position);
entry.setChecked(!entry.isChecked());
}
});
First of all don't set the list checked or unchecked on view position.
because view position means only visible items position in your listview but you would like to set checked or uncheked status on a particular list item.
That's why this problem arises in your code.
You have the need to set the items checked and unchecked on your custom arraylist getter setter like the code i have attached below:
package com.app.adapter;
public class CategoryDynamicAdapter {
public static ArrayList<CategoryBean> categoryList = new ArrayList<CategoryBean>();
Context context;
Typeface typeface;
public static String videoUrl = "" ;
Handler handler;
Runnable runnable;
// constructor
public CategoryDynamicAdapter(Activity a, Context context, Bitmap [] imagelist,ArrayList<CategoryBean> list) {
this.context = context;
this.categoryList = list;
this.a = a;
}
// Baseadapter to the set the data response from web service into listview.
public BaseAdapter mEventAdapter = new BaseAdapter() {
#Override
public int getCount() {
return categoryList.size();
}
#Override
public Object getItem(int position) {
return categoryList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
class ViewHolder {
TextView title,category,uploadedBy;
ImageView image;
RatingBar video_rating;
Button report_video ,Flag_video;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder vh = null ;
if(convertView == null) {
vh = new ViewHolder();
convertView = LayoutInflater.from(context).inflate (R .layout.custom_category_list_layout,null,false);
vh.title = (TextView) convertView .findViewById (R.id.title);
vh.image = (ImageView) convertView.findViewById(R.id.Imagefield);
convertView.setTag(vh);
}
else
{
vh=(ViewHolder) convertView.getTag();
}
try
{
final CategoryBean Cb = categoryList.get(position);
//pay attention to code below this line i have shown here how to select a listview using arraylist getter setter objects
String checkedStatus = Cb.getCheckedStringStaus();
if(checkdStatus.equal("0")
{
System.out.println("Listview not selected ");
//CK.get(arg2).setChecked(false);
checkBox.setChecked(false);
v.setSelected(false);
}
else ////checkdStatus.equal("1")
{
System.out.println("Listview selected ");
//CK.get(arg2).setChecked(true);
checkBox.setChecked(true);
v.setSelected(true);
}
catch (Exception e)
{
e.printStackTrace();
}

Categories

Resources