0

我有一个使用简单适配器构建的自定义列表视图。当我单击列表视图上的项目时,我想要该项目上的不同项目。示例:我的列表视图有两个文本字段和一个 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;
    }
}

}

4

2 回答 2

0

只需使用传递的视图(在您的情况下为 arg1):

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

由于您尚未发布整个代码,因此很可能您也可以使用传递的位置。

高温高压

于 2012-11-28T19:26:17.790 回答
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;
}
于 2012-11-28T19:47:20.683 回答