tags:

views:

39

answers:

3

Hello, I'm having trouble changing the data held within a TextView inside of a listView. I can access the data and send a toast of the text, but the list isn't updated when I change it.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    ListAdapter list_adapter = lv.getAdapter();
    TextView var_x = (TextView) list_adapter.getView(0,null,null);
    ListView list = getListView();
    int count = list.getChildCount();
    Toast.makeText(getApplicationContext(),(var_x).getText(),Toast.LENGTH_SHORT).show();
    var_x.setText("not a president");
}

How can I change the text/styling of a row once I've already created it?

Thanks for any help

A: 

Try this:

adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, presidents) setListAdapter(adapter);

// make the changes in the list

adapter.notifyDataSetChanged(); // refresh

ilarele
I tried this, but it didn't seem to make any difference.
Justin
A: 

Hi,

Hope this will work..

ArrayAdapter adapter = new ArrayAdapter<String>(this, ..., ...));

ListView resultList = (ListView) findViewById(R.id.resultList);  
resultList.setAdapter(adapter);

TextView text = (TextView) adapter.getView(0, null, null); // as an example, use the first element in the list  

Spannable str = (Spannable) text.getText();  
str.setSpan(new StyleSpan(Typeface.BOLD), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // whatever changes you want to make, this is just an example

Thanks,
Sen

Sen
Hi, thanks for responding. This line:
Justin
Spannable str = (Spannable) text.getText();
Justin
causes an error. Are you assuming that I created R.id.resultlist somewhere else? Is that statement effectively different than my call to getListView()?
Justin
A: 
ListView list = (ListView) findViewById(R.id.list);
adapter = new MyAdapter(this,R.layout.main,
            R.id.row_text,
            new String[]{"one", "two", "three"} );
list.setAdapter(adapter);

//Custom Adapter class

private class MyAdapter extends ArrayAdapter<String>{

    String[] mStrings;
    LayoutInflater mInflater; 

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] strings) {
        super(context, resource, textViewResourceId, strings);
        mStrings = strings;
        mInflater = (LayoutInflater) FirstAct.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = mInflater.inflate(R.layout.main,null, false);

        TextView text = (TextView) view.findViewById(R.id.row_text);
        text.setText(mStrings[position]);

        Spannable str = (Spannable) text.getText();
        str.setSpan(new StyleSpan(Typeface.BOLD), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return view;
    }

}

Hope this code will give you more idea (than the older one which is a bit complex), on how to get hold of each element in the list.
Just try and send me your comments.

Thanks,
Sen

Sen
Thanks again, I'm working on getting this code running. I had a few questions. Where does R.id.list come from, are you assuming I created it elsewhere? Also, what is the FirstAct object?
Justin