sergi
sergi

Reputation: 979

How can I change one specific row from a ListView?

I've searched dozens of manuals and tutorials but I haven't managed to do it.

What I need is just change the background / textcolor of a row from a ListView.

It's only one row different from the others but it can be in any position (which I already know) so I just need to know how to acces to the desired row..

I'm sorry if it's been already asked/answered but I haven't find anything that solves my case.

Thanks.

Upvotes: 0

Views: 3638

Answers (5)

Amit kumar
Amit kumar

Reputation: 143

main.xml

java file package webkul.com.todo;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;

public class add extends Activity { private ListView Lview;

private String a[]={"aaa","aaaaafddf","bcvnxc"};
  static ArrayList <String>arlst=new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    Button next = (Button) findViewById(R.id.Button02);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }

    });
    Button next1=(Button)this.findViewById(R.id.Button01);
    final EditText aa=(EditText) findViewById(R.id.edtInput);
    //final ArrayList arlst=new ArrayList();
    next1.setOnClickListener(new OnClickListener(){
        public void onClick(View view){

            String a1=aa.getText().toString();
            arlst.add(a1);
            aa.setText("");

            //arlst.add("aa2");
            //Toast.makeText(add.this, "Position=" +a1, Toast.LENGTH_SHORT).show();
            Intent myIntent = new Intent(view.getContext(), Todo.class);
            startActivityForResult(myIntent, 0);



        }

    });


}

}

Upvotes: 1

Phyrum Tea
Phyrum Tea

Reputation: 2653

You might try to extend the ListAdapter you are currently using. Overriding then getView method to intercept the view that the ListActivity is using to display a visible row.

Maybe something like the following:


public class MyListAdapter extends SimpleCursorAdapter {
  public MyListAdapter(Context context, int layout, Cursor c, String[] from,
      int[] to) {
    super(context, layout, c, from, to);
  } 
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    if (v != null && position == 2) {
      v.setBackgroundColor(Color.YELLOW);
    }
    return v;
  }
}

Upvotes: 0

pedr0
pedr0

Reputation: 3011

I have had the same issues and I agree with you, this topic isn't too much explained in the most common android manual.

I explain how I did for draw different type of row, you have to extends an Baseadapter and override :

getItemViewType(position)

getViewTypeCount(position)

getView()

If you have two type of rows you have to return alway 2 in getViewTypeCount(), in the getItemViewType() you have to return the number of view for the element present on your adapter ( an ArrayList tipically.

See this for a complete example : http://android.amberfog.com/?p=296

Upvotes: 2

John J Smith
John J Smith

Reputation: 11923

In your ListActivity class, which manages the listview, you will have to override the method onListItemClick - modify the colour here:

@Override
public void onListItemClick(ListView parent, View v, int position, long id)
{       
    v.setBackgroundColor(Color.BLUE);
}

You can use your own colours stored in your resource file.

Upvotes: 0

Kevin Coppock
Kevin Coppock

Reputation: 134684

From what I understand, since views in a ListView are recycled, if the specific row you're trying to access is not visible, it's not possible to modify it as it doesn't actually exist at that time. Can you be more descriptive on what your actual goal is with this? If it's dependent on the content of the cell, it might be better to code a check into your getView() method of your adapter to check the specific view for that content, and color the background if it does contain it.

Upvotes: 2

Related Questions