2010-09-27 91 views
0

我在更改ListView中TextView內部的數據時遇到問題。 我可以訪問數據併發送文本的祝詞,但是當我更改它時,列表不會更新 。訪問ListView中的行

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"); 
} 

我已經創建了一行後,如何更改行的文本/樣式?

感謝所有幫助

回答

0

試試這個:

適配器=新ArrayAdapter(這一點,android.R.layout.simple_list_item_1,總裁) setListAdapter(適配器);

//使列表

adapter.notifyDataSetChanged()的變化; //刷新

+0

我嘗試這樣做,但它似乎沒有任何區別。 – Justin 2010-09-28 13:46:18

0

希望這將工作..

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 

感謝,

+0

嗨,感謝您的回覆。此行: – Justin 2010-09-28 13:08:46

+0

Spannable str =(Spannable)text.getText(); – Justin 2010-09-28 13:09:29

+0

會導致錯誤。你是否假設我在其他地方創建了R.id.resultlist?這個語句與我對getListView()的調用有什麼不同嗎? – Justin 2010-09-28 13:10:46

0
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; 
    } 

} 

希望這段代碼會給你更多的想法(比老一個這是一個有點複雜),關於如何獲取列表中的每個元素。
只需嘗試並將您的意見發送給我。

感謝,

+0

再次感謝,我正在努力讓代碼運行。我有幾個問題。 R.id.list從哪裏來,你是否假設我在其他地方創建了它?另外,FirstAct對象是什麼? – Justin 2010-09-28 15:56:05