我有一个使用简单适配器构建的自定义列表视图。当我单击列表视图上的项目时,我想要该项目上的不同项目。示例:我的列表视图有两个文本字段和一个 ImageView 字段。因此,当我单击列表视图上的项目时,如何分别获取第一个文本和第二个文本。
WeatherAdapter adapter = new WeatherAdapter(this,R.layout.listview_header_row, weather_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setClickable(true);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//want to get the specific items...like R.id.txt1 and R.id.rtxt1
}
});
这是我定义的 Listview 和持有者......
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 = (ImageView)row.findViewById(R.id.imageView1);
holder.txtTitle = (TextView)row.findViewById(R.id.txt1);
holder.txtRating=(TextView)row.findViewById(R.id.rtxt1);
row.setTag(holder);
}
else
{
holder = (WeatherHolder)row.getTag();
}
Weather weather = data[position];
holder.txtTitle.setText(weather.wtitle);
//holder.imgIcon.setImageResource(weather.wicon);
holder.imgIcon.setImageDrawable(weather.wicon);
holder.txtRating.setText(weather.wrating);
return row;
}
static class WeatherHolder
{
//Bitmap imgIcon;
ImageView imgIcon;
TextView txtTitle;
TextView txtRating;
}
}
持有人的价值观是..
public class Weather {
public Drawable wicon;
public String wtitle;
public String wrating;
public Weather(){
super();
}
public Weather(String icon, String title,String rating) {
super();
//this.wicon=LoadImageFromWeb(icon);
this.wicon=LoadImageFromWebOperations(icon);
this.wtitle = title;
this.wrating=rating;
}
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
}