从自定义列表视图项

从自定义列表视图项

本文介绍了从自定义列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的适配器内置自定义列表视图。
当我点击列表视图的项目,我想在该项目上的不同项目。
例如:我的列表视图中有两个文本框和一个ImageView的领域。
所以,当我点击列表视图的项目,我如何获得单独第一个文本,并分别在第二个。

I have a custom listview built with a simple adapter.When i click the item on the listview, i want the different items on that item.Example: my listview has two text fields and a ImageView field.So when i click the item on the listview, how do i get the first text separately and the second one separately.

    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和持有人...

And this is my Listview defined and the holder...

    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;
    }
}

}

推荐答案

您既可以找你在查看重新presenting第二个参数要的意见

You could either look for the views you want in the View representing the second parameter:

@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();
}

或者你可以使用方法的第一个参数,如果你正确实施适配器:

or you could use the getItemAtPosition method on the first parameter if you properly implemented the adapter:

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

这篇关于从自定义列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:25