2012-12-04 129 views
0

這裏是我的arrayAdapter,其中有兩個文本字段「imgen」和「txtTitle」。目前我將以下文本設置爲從arrayAdapter的「imgen」textView動態更新listadapter

holder.imgIcon.setText(Double.toString(weather.getBuyingRate())); 

下面是適配器。

package com.example.mainactivity; 

import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

public class WeatherAdapter extends ArrayAdapter<Weather>{ 

    Context context; 
    int layoutResourceId;  
    Weather data[] = null; 



    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) { 
     super(context, layoutResourceId, data); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.data = data; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     WeatherHolder holder = null; 

     if(row == null) 
     { 
      LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
      row = inflater.inflate(layoutResourceId, parent, false); 

      holder = new WeatherHolder(); 
      holder.imgIcon = (TextView)row.findViewById(R.id.imgen); 
      holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); 

      row.setTag(holder); 
     } 
     else 
     { 
      holder = (WeatherHolder)row.getTag(); 
     } 

     Weather weather = data[position]; 
     holder.txtTitle.setText(weather.getName()); 
     holder.imgIcon.setText(Double.toString(weather.getBuyingRate())); 

     return row; 
    } 

    static class WeatherHolder 
    { 
     TextView imgIcon; 
     TextView txtTitle; 
    } 
} 

根據用戶是否按下按鈕,我想從主類中更改此項操作?我想要在"setText(Double.toString(weather.getBuyingRate()));"setText(Double.toString(weather.getNrOfSharesInPortfolio()));之間進行切換,具體取決於goButton當前具有的文本。請參閱下面的代碼。

注意這應該更新列表中每個元素的imgIcon。

有沒有辦法從MainActitivy訪問「holder.imgIcon」?

這裏是我的MainActivity

package com.example.mainactivity; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ListView; 

public class MainActivity extends Activity { 

    private ListView listView1; 
    private Button goButton; 
    private String[] listheader = {"Köp Kurs","Antal"}; 
    int totalElemInlist = listheader.length; 
    int currentelemInList=0; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Weather weather_data[] = new Weather[] 
     { 
      new Weather("ABB", 56.0, 300), 
      new Weather("Volvo", 89.0,500), 
      new Weather("Astra Zeneca", 98.55, 50) 
     }; 

     WeatherAdapter adapter = new WeatherAdapter(this, 
       R.layout.listview_item_row, weather_data); 


     listView1 = (ListView)findViewById(R.id.listView1); 

     View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); 
     listView1.addHeaderView(header); 
     goButton = (Button) findViewById(R.id.testButton); 
     goButton.setText(listheader[currentelemInList]); 

     listView1.setAdapter(adapter); 

     goButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) { 
       String buttonPressed = (String) ((Button) view).getText(); 
       goThroughList(buttonPressed); 
       System.out.println("Button Clicked" + buttonPressed); 
      } 
      }); 
    } 

    private void goThroughList(String buttonPressed){ 
     currentelemInList++; 
     if(currentelemInList>=totalElemInlist){ 
      currentelemInList=0; 
     } 
     goButton.setText(listheader[currentelemInList]); 

     if(buttonPressed == "Köp Kurs"){ 
      System.out.println("Köp kurs"); 
      //holder.imgIcon.setText(Double.toString(weather.getBuyingRate())); 
     } 
     else if(buttonPressed == "Antal"){ 
      System.out.println("Antal"); 
      //holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio())); 
     } 




     System.out.println(currentelemInList); 
    } 
} 

回答

0

你有一個TextView imgIcon這將顯示任一buyingRate或nrOfSharesInPortfolio。按下goButton時切換顯示。

public class WeatherAdapter extends ArrayAdapter<Weather>{ 

    // ... 

    private String buttonPressed = "Köp Kurs"; 

    public void setButtonPressed(String s) { 
     buttonPressed = s; 
     notifyDataSetChanged(); 
    } 

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

     // ... 

     Weather weather = data[position]; 
     holder.txtTitle.setText(weather.getName()); 

     if(buttonPressed == "Köp Kurs"){ 
      holder.imgIcon.setText(Double.toString(weather.getBuyingRate())); 
     } 
     else if(buttonPressed == "Antal"){ 
      holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio())); 
     } 

     return row; 
    } 

    // ... 
} 

在MainActivity.onCreate(),設置goButton上點擊監聽器:

goButton.setOnClickListener(new OnClickListener() { 
    public void onClick(View view) { 
     String buttonPressed = (String) ((Button) view).getText(); 
     adapter.setButtonPressed(buttonPressed); 
    } 
});