2012-11-28 104 views
0

我有一個自定義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; 
    } 
} 

}

回答

0

只是使用傳入查看(arg1在你的情況下):

WeatherHolder holder = (WeatherHolder) arg1.getTag(); 

由於您尚未發佈完整的代碼,因此很可能您也可以使用傳遞的位置。

HTH

+0

嘿我已更新完整的代碼的帖子...請再次檢查......因爲你有什麼建議墜毀 –

0

你既可以找你要在View代表第二個參數的觀點:

@Override 
public void onItemClick(AdapterView<?> parent, View rowView, int position, long id) { 
    String firsttext = ((TextView) rowView.findViewById(R.id.txt1)).getText().toString(); 
    String secondtext = ((TextView) rowView.findViewById(R.id.rtxt1)).getText().toString(); 
} 

或者你可以使用的第一個參數getItemAtPosition方法,如果你正確地實現適配器:

@Override 
public void onItemClick(AdapterView<?> parent, View rowView, int position, long id) { 
    Weather rowData = (Weather) parent.getItemAtPosition(position);  
    // get the data from rowData; 
}