我有一個自定義listview用一個簡單的適配器。 當我點擊列表視圖上的項目時,我想要該項目上的不同項目。 例如:我的listview有兩個文本字段和一個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;
}
}
}
嘿我已更新完整的代碼的帖子...請再次檢查......因爲你有什麼建議墜毀 –