我是Android的新手。我的代碼很困難。 這是我的問題。 我的MainActivity中有一個TextView,它擴展了活動。而這個textview已經從另一個擴展Adapter類並實現GestureDetector的類中進行了更新。如何從不擴展的類更新TextView活動類
基於拋出事件,我正在更新我的textview。我怎樣才能做到這一點。
我是Android的新手。我的代碼很困難。 這是我的問題。 我的MainActivity中有一個TextView,它擴展了活動。而這個textview已經從另一個擴展Adapter類並實現GestureDetector的類中進行了更新。如何從不擴展的類更新TextView活動類
基於拋出事件,我正在更新我的textview。我怎樣才能做到這一點。
我已經解決了我的問題atlast。我在下一行中添加了以下行。所以現在當這個方法被調用時,我的textView會自動更改它的文本。
mymainActivity.textView_name.setText((new myCurrentclass_Name(this.context_name)).some_array[value]);
這是我如何被允許改變我在我的MainActivity中的TextView的文本。
你可以讓你TextView static
或者你可以採取現場在AdapterClass
並傳遞TextView
到AdapterClass
的構造,而你已經完成了所有的AdapterClass
您可以更新TextView
從AdapterClass
編輯:
說,這是你的AdapterClass:
public class AdapterClass extend BaseAdater..(blah blah)
{
TextView textView;
public AdapterClass(Context context, TextView view)
{
this.context = context;
this.textView = view;
}
public View getView(int position, View convertView, ViewGroup viewGroup)
{
//do your task
textView.setText("123 Results found");
}
}
和我n您的活動,同時宣佈AdapterClass 通過你的TextView像構造:
AdapterClass adapter = new AdapterClass(this, textView);
在適配器類創建一個域,並指定你的文本視圖的情況下,或使用MainActivity.this.findViewById(...)或者只要找到ViewById(...)如果你的Adapter類是你的MainActivity的內部非靜態類。
protected void onCreate(...) {
...
//I suppose you get your textView like this way
TextView textView = ((TextView) findViewById(...));
//I suppose your MainActivity extends from ListActivity
getListView().setAdapter(new YourAdapter(textView));
}
private static class YourAdapter extends BaseAdapter {
private TextView textView;
private YourAdapter(TextView textView) {
this.textView = textView;
}
...
//somewhere
this.textView.setText(".....");
}
或
// Inner non-static class
private class YourAdapter extends BaseAdapter {
...
//somewhere
((TextView) findViewById(...)).setText("...");
}
我可以只有這個簡單的代碼片段。我無法得到它。 – 2011-04-28 05:14:30
爲你寫了一些片段:) – 2011-04-28 05:38:28
這真的很有幫助。非常感謝.. – 2011-04-28 06:21:01
你可以用簡單的代碼來解釋它嗎?我無法得到你。 – 2011-04-28 05:20:22
感謝您的快速回復。但是,如果我的類擴展了Abstract類而不是某個適配器,這個代碼是否可以工作。在這種情況下會發生什麼。 – 2011-04-28 06:05:42
那麼BaseAdapter是一個抽象類,所以肯定會工作。 – 2011-04-28 06:11:34