BaseAdapter not populating listview - android

So i have a base adapter class that I built. I also try to use this in one of my activities. I have a xml for the parent layout and an xml for the rows to inflate. However, these components are not linking together. The app doesnt crash but the rows are not inflated (although the parent xml file still displays on the creen). Could some look at my code and tell me what critical component i am overlooking? I will only post the relevant code. Cant post the error logs since no errors are occurring. Thanks very much
Parent XML (nearby_places_list)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical"
android:background="#drawable/bg">
<Button
android:id="#+id/continuetomap"
android:layout_height="100dp"
android:layout_width="match_parent"
android:background="#drawable/map_continue"
/>
<ListView
android:id="#+id/list_view_nearby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:listSelector="#drawable/list_selector">
</ListView>
</LinearLayout>
Row to Inflate XML (nearby_list_row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:background="#drawable/list_selector">"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/list_selector">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/list_selector">
<TextView
android:id= "#+id/name"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="name"
android:textColor="#54944d"/>
<TextView
android:id= "#+id/vicinity"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="vicinity"
android:textColor="#000000"/>
<TextView
android:id= "#+id/openorclosed"
android:textSize="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open"
android:textColor="#000000"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.74" >
<Button
android:layout_width="60dp"
android:id="#+id/button_add_to_fav"
android:background="#drawable/add_square"
android:layout_height="60dp"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="60dp"
android:id="#+id/button_call_rest"
android:background="#drawable/add_square"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/button_add_to_fav"
/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Base Adapter Class
public class AdapterForList extends BaseAdapter {
private final Context context;
private final ArrayList<HashMap<String, String>> alHm;
private final String TAG_A;
private final String TAG_B;
private final String TAG_C;
public AdapterForList(Context context, ArrayList<HashMap<String, String>> alhm, String TAG_1, String TAG_2, String TAG_3) {
super();
this.context = context;
this.alHm = alhm;
TAG_A = TAG_1;
TAG_B = TAG_2;
TAG_C = TAG_3;
}
#Override
public View getView(int position, View convertView , ViewGroup parent){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.nearby_list_row, parent, false);
TextView tv1 = (TextView) convertView.findViewById(R.id.name);
TextView tv2 = (TextView) convertView.findViewById(R.id.vicinity);
TextView tv3 = (TextView) convertView.findViewById(R.id.openorclosed);
Button btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav);
Button btnCall= (Button) convertView.findViewById(R.id.button_call_rest);
if (alHm.get(position).containsKey(TAG_A)){ // get the value from the key (if it exist) and set the first text view to it
String value = alHm.get(position).get(TAG_A);
tv1.setText(value);;
}
if (alHm.get(position).containsKey(TAG_B)){
String value = alHm.get(position).get(TAG_B);
tv2.setText(value);;
}
if (alHm.get(position).containsKey(TAG_C)){ // get the value from the key (if it exist) and set the first text view to it
String value = alHm.get(position).get(TAG_C);
tv3.setText(value);;
}
return convertView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return alHm.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Relevant activity code
protected void onPostExecute(Boolean result){
if (result){
// adding data to listview
final ListView lv = (ListView) findViewById(R.id.list_view_nearby);
lv.setAdapter(new AdapterForList(context, listOfHM, JSONextractor.TAG_NAME,
JSONextractor.TAG_VICINITY, JSONextractor.TAG_OPERATINGHOURS_OPENNOW));
//more code ...
}
}

So it turns out my code did work (but not optimally). I posted below how to correctly use the base adapter
Just read the article (really opened my mind) posed by savadow. basically the way i have my BaseAdapter im not utilizing any of the optimization ability of the class. Instead I will change the code to the following in the base adapter class.
http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
public View getView(int position, View convertView , ViewGroup parent){
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = inflator.inflate(R.layout.nearby_list_row, parent, false);
}
// now if the convertView is not null i simply update the values rather than re-inflate the view (inflating is expensive)
tv1 = (TextView) convertView.findViewById(R.id.name);
tv2 = (TextView) convertView.findViewById(R.id.vicinity);
tv3 = (TextView) convertView.findViewById(R.id.openorclosed);
btnAdd= (Button) convertView.findViewById(R.id.button_add_to_fav);
btnCall= (Button) convertView.findViewById(R.id.button_call_rest);

Related

OnClickListener on Button in ListView changes multiple Items

so i declared a button for every item in the ListView but it happens that it responds to more Items then just the clicked one. For example if i push the button of the first button the 9th and 18th item are getting changed aswell. It would be awesome if you could help me with this one :) thanks in advance
This is the CusomtListAdapter which fills the ListView
public class CustomListAdapter extends BaseAdapter {
private ArrayList<Task> tasks;
private LayoutInflater inflater = null;
private Application application;
public CustomListAdapter(Activity activity, Application application,
ArrayList<Task> tasks) {
this.application = application;
this.tasks = tasks;
inflater = (LayoutInflater) application.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
TextView lv_tv_description;
TextView lv_tv_period;
Button lv_bt_done;
TextView lv_gone_pk;
TextView lv_gone_group_pk;
TextView lv_gone_description;
TextView lv_gone_period;
TextView lv_gone_period_kind;
TextView lv_gone_time;
TextView lv_gone_done;
}
#Override
public int getCount() {
return tasks.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View vi, ViewGroup parent) {
final ViewHolder viewHolder;
if (vi == null) {
vi = inflater.inflate(R.layout.listview, parent, false);
viewHolder = new ViewHolder();
viewHolder.lv_tv_description = (TextView) vi.findViewById(R.id.lv_tv_description);
viewHolder.lv_tv_period = (TextView) vi.findViewById(R.id.lv_tv_period);
viewHolder.lv_bt_done = (Button) vi.findViewById(R.id.lv_bt_done);
viewHolder.lv_gone_pk = (TextView) vi.findViewById(R.id.lv_gone_pk);
viewHolder.lv_gone_group_pk = (TextView) vi.findViewById(R.id.lv_gone_group_pk);
viewHolder.lv_gone_description = (TextView) vi.findViewById(R.id.lv_gone_description);
viewHolder.lv_gone_period = (TextView) vi.findViewById(R.id.lv_gone_period);
viewHolder.lv_gone_period_kind = (TextView) vi.findViewById(R.id.lv_gone_period_kind);
viewHolder.lv_gone_time = (TextView) vi.findViewById(R.id.lv_gone_time);
viewHolder.lv_gone_done = (TextView) vi.findViewById(R.id.lv_gone_done);
vi.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) vi.getTag();
}
viewHolder.lv_tv_description.setText(tasks.get(position).getDescription());
viewHolder.lv_tv_period.setText(tasks.get(position).getPeriod_kind());
viewHolder.lv_bt_done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DatabaseHandler dbhandler = new DatabaseHandler(application);
View pk_view = (View) v.getParent().getParent().getParent();
TextView textview_pk = ((TextView) pk_view.findViewById(R.id.lv_gone_pk));
View parentView = (View) v.getParent().getParent();
TextView textview1 = ((TextView) parentView.findViewById(R.id.lv_tv_description));
TextView textview2 = ((TextView) parentView.findViewById(R.id.lv_tv_period));
Button button = ((Button) parentView.findViewById(R.id.lv_bt_done));
if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("done")) {
viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
viewHolder.lv_bt_done.setText("UNDO");
viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent);
notifyDataSetChanged();
dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "true");
} else if (viewHolder.lv_bt_done.getText().toString().equalsIgnoreCase("undo")) {
viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
viewHolder.lv_gone_description.setPaintFlags(viewHolder.lv_gone_description.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
viewHolder.lv_bt_done.setText("DONE");
viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background);
notifyDataSetChanged();
dbhandler.updateDone(Integer.valueOf(textview_pk.getText().toString()), "false");
}
dbhandler.close();
}
});
viewHolder.lv_gone_pk.setText(String.valueOf(tasks.get(position).getPk()));
if (tasks.get(position).getDone().equalsIgnoreCase("true")) {
viewHolder.lv_tv_description.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
viewHolder.lv_tv_period.setPaintFlags(viewHolder.lv_tv_description.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
viewHolder.lv_bt_done.setText("UNDO");
viewHolder.lv_bt_done.setBackgroundResource(R.drawable.layout_rounded_background_accent);
}
return vi;
}}
and this is the matching layout file for all the items
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="70dp">
<LinearLayout
android:focusable="false"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:focusable="false"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<RelativeLayout
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:focusable="false"
android:id="#+id/lv_tv_description"
android:textSize="20dp"
android:textColor="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="7dp"/>
</RelativeLayout>
<RelativeLayout
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<TextView
android:focusable="false"
android:id="#+id/lv_tv_period"
android:textColor="#android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4">
<Button
android:id="#+id/lv_bt_done"
android:text="DONE"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:background="#drawable/layout_rounded_background"/>
</RelativeLayout>
</LinearLayout>
<!-- invisible TextViews -->
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_pk"
android:visibility="visible"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_group_pk"
android:visibility="visible"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_description"
android:visibility="gone"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_period"
android:visibility="gone"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_period_kind"
android:visibility="gone"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_time"
android:visibility="gone"
android:layout_alignParentRight="true"/>
<TextView
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lv_gone_done"
android:visibility="gone"
android:layout_alignParentRight="true"/>
</RelativeLayout>
if you need anything else please let me know in the comments :)
The View gets reused in the ListView via the second parameter (convertView) of the getView() method.
From the documentation,
convertView - The old view to reuse, if possible. Note: You should
check that this view is non-null and of an appropriate type before
using. If it is not possible to convert this view to display the
correct data, this method can create a new view.
So at any time, only the number of views (plus a couple more) that are visible on your device screen is available in memory. As you scroll the list, the views that go off the screen are reused for the items that are coming into the screen.
In your case, the 1st, 9th item and 18th item are the same View being reused. That's why you are getting the changed View for the 9th and 18th item when you press the button only on the 1st item.
Solution:
When the button is pressed, add that state information in some data structure. For example, in an array of booleans, save true for the item whose button is in pressed state and false for other buttons.
And in the getView() method, check the state of the button from that array of booleans and change the view respectively.

How to add a white line below every ListView Item in xml?

My Problem is --> I put a white line as a view in the Fragment item xml but it doesn't appear when i debug it. How can i get the changes i make in the fragment item xml file. Because i tried some little changes like alpha property change but it didnt change is it because of this is a listview and has itself design? Please help me about that problem.
The xml code and adapter code are below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/menulogo"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/infoicon"
android:layout_gravity="center"
android:paddingTop="5dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/txtMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Company Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/line"
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="#+id/txtMenu"
android:background="#FFFFFF"
/>
</LinearLayout>
My listview adapter codes right down below.
public class MenuListAdapter extends ArrayAdapter<Menu_Item> {
private final ArrayList<Menu_Item> list;
private final Activity context;
private int[] colors = new int[]{0x34a4a4a4, 0x34fafafa};
public MenuListAdapter(Activity context, ArrayList<Menu_Item> list) {
super(context, R.layout.menu_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
public ImageView menulogo;
public TextView txtMenu;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.menu_item, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.menulogo = (ImageView) view.findViewById(R.id.menulogo);
viewHolder.txtMenu = (TextView) view.findViewById(R.id.txtMenu);
view.setTag(viewHolder);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtMenu.setText(list.get(position).getMenuName());
//holder.menulogo.setImageBitmap(list.get(position).getMenuImage());
String menuName=list.get(position).getMenuName();
if (menuName.equals("Info")){
holder.menulogo.setImageResource(R.drawable.infoicon);
}
if (menuName.equals("Odalar")){
holder.menulogo.setImageResource(R.drawable.accomodation);
}
if (menuName.equals("Galeri")){
holder.menulogo.setImageResource(R.drawable.galeryicon);
}
if (menuName.equals("Aktiviteler")){
holder.menulogo.setImageResource(R.drawable.activitiesicon);
}
if (menuName.equals("Robin's Kids Club")){
holder.menulogo.setImageResource(R.drawable.kidsclub);
}
if (menuName.equals("Restaurants")){
holder.menulogo.setImageResource(R.drawable.restaurantsicon);
}
view.setId(list.get(position).getMenu_id());
return view;
}
#Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
}
I found an answer to my question and wanted to share it with you.
I only used these 2 properties below of ListView
android:divider="#FFFFFF"
android:dividerHeight="1dip"
Like this...
<ListView
android:id="#+id/menuList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="#FFFFFF"
android:dividerHeight="1dip"
/>
In ListView XML add these two lines
<ListView
android:divider="#android:color/white"
android:dividerHeight="5dip"
.
.
</ListView>
Try to change the View with id line for a Space component. It is an extension of View, and works better for what you want.
Here is a description of the widget:
https://developer.android.com/reference/android/widget/Space.html

Row in listview not preserve the makeover and appears repeated

I have a list that when you click on the row on the right side the layout with the icon is changed by other. This makes it well but when I scrolling in the list, I notice there are rows with the same icon without do click. Also, if I scroll quickly these same icons are lost and are built differently. I think it's on the reuse of cells but I can not find the solution. What I want is that the image stays active only in the row where clicka.
Thanks!!
Adapter class:
public class ListadoVotantesArrayAdapter extends ArrayAdapter<Votante> {
private Context context;
private LayoutInflater inflater;
private ArrayList<Votante> listaVotantes;
private int rowLayout;
private View mConverView;
public ListadoVotantesArrayAdapter(Context context, int resource, ArrayList<Votante> objects) {
super(context, resource,objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
this.listaVotantes = objects;
this.rowLayout = resource;
}
public int getCount(){
return this.listaVotantes.size();
}
public Votante getVotante(int position){
return this.listaVotantes.get(position);
}
private static class ViewHolder {
public TextView lblName;
public TextView lblLastName;
public TextView lblDni;
public TextView lblVoterNumber;
public RelativeLayout lytVoted;
public RelativeLayout lytCanVote;
public RelativeLayout lytUndo;
}
public View getView(int position, View converView, ViewGroup parent) {
final ViewHolder viewHolder;
mConverView = converView;
if (mConverView == null){
viewHolder = new ViewHolder();
this.mConverView = inflater.inflate(this.rowLayout, parent, false);
if (mConverView != null){
viewHolder.lblName = (TextView) mConverView.findViewById(R.id.lblVoterName);
viewHolder.lblLastName = (TextView) mConverView.findViewById(R.id.lblVoterLastName);
viewHolder.lblDni = (TextView) mConverView.findViewById(R.id.lblDni);
viewHolder.lblVoterNumber = (TextView) mConverView.findViewById(R.id.lblNumber);
viewHolder.lytVoted = (RelativeLayout) mConverView.findViewById(R.id.lytVoted);
viewHolder.lytCanVote = (RelativeLayout) mConverView.findViewById(R.id.lytCanVote);
viewHolder.lytUndo = (RelativeLayout) mConverView.findViewById(R.id.lytUndo);
mConverView.setTag(viewHolder);
}
}else{
viewHolder = (ViewHolder) mConverView.getTag();
}
Votante votante = getVotante(position);
viewHolder.lblName.setText(votante.getNombre());
viewHolder.lblLastName.setText(votante.getApellidos());
viewHolder.lblDni.setText(votante.getDni());
viewHolder.lblVoterNumber.setText(""+votante.getNumVotante());
if (votante.getVotado()){
viewHolder.lytVoted.setVisibility(View.VISIBLE);
viewHolder.lytCanVote.setVisibility(View.GONE);
}else{
viewHolder.lytVoted.setVisibility(View.GONE);
viewHolder.lytCanVote.setVisibility(View.VISIBLE);
}
mConverView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.lytUndo.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
});
return mConverView;
}
}
Layout Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_voter"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<RelativeLayout
android:id="#+id/lytVoterNumber"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:background="#color/lightBlueItemList">
<TextView
android:id="#+id/lblNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="1999"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lytVoterData"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/lytCanVote"
android:layout_toRightOf="#+id/lytVoterNumber"
android:layout_toStartOf="#+id/lytCanVote"
android:background="#color/whiteItemList"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/lblVoterLastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Suarez Garcia de la Serna"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblVoterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="José Carlos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/lblDni"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="44950962S"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<RelativeLayout
android:id="#+id/lytCanVote"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/yellowItemList"
android:minHeight="30dp"
android:minWidth="30dp">
<ImageView
android:id="#+id/imgVote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/flecha" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/lytVoted"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentRight="true"
android:background="#color/grrenAcceptList"
android:minHeight="30dp"
android:minWidth="30dp"
android:visibility="gone">
<ImageView
android:id="#+id/imgAccept"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/aceptar"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="70dp"
android:layout_height="70dp"
android:minHeight="30dp"
android:minWidth="30dp"
android:background="#color/redD3"
android:id="#+id/lytUndo"
android:layout_alignParentRight="true"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgUndo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="#drawable/undo"/>
</RelativeLayout>
</RelativeLayout>
You need to follow below Idea
1) this line of code mConverView = converView; doesn't makes sense. directly use converView(View from getView param). Remove mConvertView from the adapter.
2) Add some mechanism which can tell you which row is clicked and save that variable.
Example:- Have an boolean Array of size Rows.size . and set them in onClick.
Boolean[] array = new Boolean[getSize()];
in onClick that you already have in your getview set this.
public void onClick(View v) {
array[position] = !array[position];
viewHolder.lytUndo.setVisibility(View.VISIBLE);
//You do not need to call notifyDatasetChange.
3) in getView(), when you are setting data in to row items/or just before onClick. have a check like below.
if(array[position])// this will tell you if you need to show or hid lytUndo thing
viewHolder.lytUndo.setVisibility(View.VISIBLE); // Show it
else
viewHolder.lytUndo.setVisibility(View.GONE); // hide it or do whatever you want
You might need to set some params as final
I have written this as simple as possible, comment back with your result.

Filling ListView out

I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :
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="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#android:color/transparent" >
</LinearLayout>
And I want to add TextView to returned view in getView with following properties :
Height : fill_parent
Width : wrap_content
Text : Text View Item
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
return convertView;
}
Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?
Thanks in Advance :)
This was my getView() method.
main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/button1"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Adapter
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.getAppName().setText(str.get(position));*/
if(position==0)
{
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(R.drawable.ic_launcher);
convertView = iv;
}
else if(position==1)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("text view");
convertView = tv;
}
else
{
EditText et = new EditText(getApplicationContext());
et.setText("ET go home");
convertView = et;
}
return convertView;
}
private class ViewHolder {
private View pathRow;
private TextView appNameView = null;
public ViewHolder(View row) {
pathRow = row;
}
public TextView getAppName() {
if (null == appNameView) {
appNameView = (TextView) pathRow.findViewById(R.id.tv);
}
return appNameView;
}
}
}
The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.
I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.

ArrayAdapter - What am I doing wrong?

I have a ListActivity that I'm trying to bind to an array of business objects using a custom ArrayAdapter.
When I run the application on either the emulator or the real android device I get a null reference in the ArrayAdapter on the line "holder.txtTeam1.setText(info.Team1);". After doing some debugging it looks like the call to inflate the layout didn't actually create the child TextViews, it only created the outter TableLayout.
This is all based on a tutorial I found on the internet, and I've been banging my head off the desk for almost a week on this one. Can anyone tell me what I'm doing wrong?
The activity looks like this:
public class BaseballPressActivity extends ListActivity {
private GameDataAdapter adapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new GameDataAdapter(this, new GameData[]{new GameData("3:10pm","Team 1","Team 2")});
this.setListAdapter(adapter);
}
}
The ArrayAdapter looks like this:
public class GameDataAdapter extends ArrayAdapter<GameData> {
Context context;
GameData data[] = null;
public GameDataAdapter(Context context, GameData[] data) {
super(context,R.layout.listitem_game, R.id.Name1, data);
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
GameViewHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.test , parent, false);
holder = new GameViewHolder();
holder.txtTeam1 = (TextView)row.findViewById(R.id.Name1);
row.setTag(holder);
}
else
{
holder = (GameViewHolder)row.getTag();
}
GameData info = data[position];
holder.txtTeam1.setText(info.Team1);
return row;
}
static class GameViewHolder
{
TextView txtTeam1;
TextView txtTeam2;
TextView txtGameTime;
}
}
And the layout for the list activity item looks like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:id="#+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:text="TextView" android:layout_weight="0" android:id="#+id/textView1" android:layout_height="wrap_content" android:layout_width="0dp"></TextView>
<ImageView android:id="#+id/imageView1" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
<LinearLayout android:id="#+id/linearLayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp">
<TextView android:text="TextView" android:id="#+id/Name1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView" android:id="#+id/Name2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<ImageView android:id="#+id/imageView2" android:src="#drawable/icon" android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="0dp"></ImageView>
</TableRow>
</TableLayout>
ok, i think the problem is here
row = inflater.inflate(R.layout.test , parent, false);
change false to true, i think you have to attach the view to it's parent.
if that doesn't work try this.
row = inflater.inflate(R.layout.test , null);
Abandon ArrayLayout for SimpleAdapter or SimpleCursorAdapter. This will allow you to easily bind your data with simple to fairly complex layouts.

Categories

Resources