这是我的arrayAdapter,我有两个文本字段,“imgen”和“txtTitle”。目前我将以下文本设置为arrayAdapter
中的“imgen”textViewholder.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 :(得分:0)
你有一个TextView imgIcon,它将显示buyRate或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);
}
});