2016-11-07 33 views
-1

在同一活動佈局上,我有一個TextView字段和一個ListView,每個項目上都有一個按鈕。在我的ListView數組適配器中,我在getView()方法內部有一個按鈕單擊偵聽器,我試圖「動態」更新上面提到的TextView字段的值。如何從ListView按鈕動態更新TextView字段?

到目前爲止,我已經能夠使用虛實實例來更新TextView值,但是當我通過重新訪問主菜單中的活動頁面來刷新頁面時,我只能看到更新的TextView。

作爲最後一個解決方案,我已將它設置爲在getView()方法內的buton偵聽器內重新啓動活動頁面,而無需手動執行。這不是最好的方式,因爲用戶每次點擊ListView項目按鈕時都能看到頁面重新加載。

你能告訴我如何動態更新數值嗎?

以下是我的適配器類代碼:

所有的
public adapter(Context context, int resource, ArrayList<value> objects) 
{ 
    super(context, resource, objects); 
    layout = resource; 
    array = objects; 
} 

@Override 
public View getView(final int position, View convertView, final ViewGroup parent) 
{ 
    box = null; 
    db = new database(getContext()); 

    if (convertView == null) 
    { 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     convertView = inflater.inflate(layout, null); 

     box = new holder(); 
     box.X = (TextView) convertView.findViewById(R.id.valueName); 
     box.Y = (TextView) convertView.findViewById(R.id.valueNumber); 
     box.button = (Button) convertView.findViewById(R.id.itemDeleteButton); 
     box.button.setTag(position); 
     convertView.setTag(box); 
    } 

    else 
    { 
     box = (holder) convertView.getTag(); 
    } 

    box.X.setText(getItem(position).getX()); 
    box.Y.setText(String.valueOf(getItem(position).getY())); 

    box.button.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      View view = View.inflate(getContext(), R.layout.activity_plan, null); 

      list = (ListView) view.findViewById(R.id.listView); 
      month = (Spinner) view.findViewById(R.id.inputSpinner); 
      remainder = (TextView) view.findViewById(R.id.outputRemainder); 

      adapt = new adapter(getContext(), R.layout.activity_item, array); 
      array.remove(position); 

      month_is = month.getSelectedItem().toString(); 
      db.removePlanData(month_is, position + 1); 
      notifyDataSetChanged(); 

      cursor = db.getHelpDataRows(month_is); 
      cursor.moveToFirst(); 
      val_is = Integer.parseInt(cursor.getString(cursor.getColumnIndex("value"))); 

      remainder_is = val_is - db.getCurrent(month_is); 
      remainder.setText(String.valueOf(remainder_is)); 

      // RESTARTING ACTIVITY TO SEE THE UPDATED TEXTVIEW VALUE OUTSIDE LISTVIEW. 
      Intent intent= new Intent(getContext(), plan.class); 
      getContext().startActivity(intent); 
     } 
    }); 

    return convertView; 
} 


public class holder 
{ 
    TextView X; 
    TextView Y; 
    Button button; 
} 
+1

它會是有用的,如果你能證明你的代碼 –

+0

顯示你的代碼。 – Chisko

+0

感謝您的回覆。我已經在下面發佈了我的代碼。這麼晚纔回復很抱歉。我感謝您的幫助! – Ras

回答

0

首先,如果你寫一個大寫字母開頭的類名會更好。接下來,據我瞭解,您的情況是:當您單擊listView的項目上的按鈕時,您應該能夠更改listView以外的textView的文本。可以通過在活動中使textViewpublic static完成,以便您可以從其他類訪問。對於前:

MainActivity.textView.setText("something"); 

在您的主要活動做出的TextView public static

public static TextView textView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.viloyatlat_frame); 

    textView = (TextView)findViewById(R.id.textView); 

} 
+0

謝謝,Abduaziz。我明白你的意思。讓我試試看。 – Ras

+0

如果有幫助,請接受我的回答。快樂編碼,Ras! –

+0

這樣做。謝謝! – Ras