Question

I am using Base Adapter in my listview for populating data in android. but i am just begineer in android. so, little bit confusing in base Adapter . I have display data in base Adapter.

Here, My main problem is, If i click position 0 then i want to change color of txtview of position. If i click position 5 then change color of textview of position 5 others remains same color.

Just clickable position texview change to red other white.

 if 0 - red - clicked
       1-white
       2 - white
       3-white
       4- white
       5- white

if 0 - white
       1-white
       2 - white
       3- red- clicked
       4- white
       5- white

Here Code of mine:

ArrayList<Geo> list;
public class TestListAdapter extends BaseAdapter {

    public TestListAdapter(Context context, int textViewResourceId,
            ArrayList<Geo> objects, String name_of_APP) {

        this.objects = objects;
        contx = context;
        layoutInflator = LayoutInflater.from(contx);
        _APP = name_of_APP;
        VVV = layoutInflator.inflate(R.layout.row_cell_multilevel, null);
      list=objects;
        for(int i=0;i<list.size();i++){
            itemPos.add(i);
        }
    }

//  @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        try{
            CC = getItem(position);//objects.get(position);
            if (convertView == null){

                convertView = View.inflate(contx, R.layout.row_cell_multilevel, null);
                holder = new ViewHolder();
                holder.txtName = (TextView) convertView.findViewById(R.id.row_cell_text_multilevel);
                holder.btn = (Button) convertView.findViewById(R.id.row_cell_btn_multilevel);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.btn.setTag(position);
            holder.txtName.setTag(position);

            holder.txtName.setText(CC.Name);

            holder.txtName.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        String name = holder.txtName.getText().toString();
                    Toast.makeText(contx, "You Have Choosed " + name  , Toast.LENGTH_SHORT).show();
      if (itemPos.contains(position)) {
     holder.txtName.setTextColor(Color.RED); notifyDataSetChanged();} 
         else {
     holder.txtName.setTextColor(Color.WHITE); notifyDataSetChanged();}
        }
            });


        }catch (Exception e){
            Log.d("Exception", "" + e.getMessage());
        }
        return convertView;
    }

    static class ViewHolder {
        TextView        txtName;
    }

    //@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.objects == null ? 0 : this.objects.size();
    }

    //@Override
    public Geo getItem(int position) {
        // TODO Auto-generated method stub
        return this.objects == null ? null : this.objects.get(position);
    }

//      @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public String getID() {
        Log.e(post, "post id=" + CC.ID);
        return CC.ID;
    }

}
Was it helpful?

Solution

Do not handle the click inside the Adapter getView().

In your Fragment/Activity , add a item click listener to the list view and change the text color.

Here is a simple sample code..

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        TextView previousView = null; // to hold the previous clicked view
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            TextView textView = (TextView)view.findViewById(R.id.row_cell_text_multilevel);
            if(previousView != null) {
                // revert the previous view when a new item is clicked
                previousView.setTextColor(Color.WHITE);
            }
            textView.setTextColor(Color.RED);
            previousView = textView;
        }
    });

OTHER TIPS

set setOnItemClickListener on ListView and override onItemClick method inside it

lv.setOnItemClickListener(new OnItemClickListener()
{
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{ 
    //apply switch case here for different position
    switch(position){
      case 0:
          your_textView.setTextColor(your color code);

     }
        }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top