1

我有一个列表视图并使用 LayoutInflater 概念填充列表视图。我需要从列表中获取所选项目,每次单击特定项目时,我都会获得最顶层的项目。为什么会发生。有什么办法可以得到我选择的确切项目。我附上了我的代码。

这是我的 xml 文件 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
 <ImageView
            android:id="@+id/imageheader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"

            android:src="@drawable/logo" 
            android:adjustViewBounds="true"/>
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:focusable="false"/>

</LinearLayout>

项目.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1">
<ImageView
  android:id="@+id/image"
  android:layout_width="50dip"
  android:layout_height="50dip" android:src="@drawable/stub"/>                    
<TextView
  android:id="@+id/text"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:clickable="true"
  android:onClick="Viewprofileclick"
  android:layout_weight="1" android:layout_gravity="left|center_vertical"/>           
</LinearLayout>

并且类文件是

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    strUrl = "http://192.118.0.124/Service.asmx/GethResult";
    inflater = (LayoutInflater)getApplicationContext().       getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DownloadTask downloadTask = new DownloadTask();

    downloadTask.execute(strUrl);

    list=(ListView)findViewById(R.id.list);


}

 public void Viewprofileclick(View v)
   {

    TextView txt = (TextView) findViewById(R.id.text);
      String userid=txt.getText().toString();
             //Intent intent = new Intent(Search_Result.this, ViewProfile.class);
     //intent.putExtra("Searchuserid",userid);
    //startActivity(intent);
      Toast.makeText(getApplicationContext(), userid, Toast.LENGTH_LONG).show();
   }

懒惰类

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText("item "+position);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

}

4

3 回答 3

2
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
    vi = inflater.inflate(R.layout.item, null);
    holder=new ViewHolder();
    holder.text=(TextView)vi.findViewById(R.id.text);
    holder.image=(ImageView)vi.findViewById(R.id.image);
    vi.setTag(holder);
}
else
    holder=(ViewHolder)vi.getTag();

holder.text.setText("item "+position);
holder.image.setTag(data[position]);
imageLoader.DisplayImage(data[position], activity, holder.image);
return vi;
 }



convertView.setOnClickListener(new OnItemClickListener(position));
}

private class OnItemClickListener implements OnClickListener {
    private int mPosition;

    OnItemClickListener(int position) {
        mPosition = position;
    }

    public void onClick(View v) {

        Toast.makeText(getApplicationContext(), mPosition,
                Toast.LENGTH_LONG);

    }
}
于 2013-11-01T11:55:48.050 回答
1

利用list.setOnItemClickListener

于 2013-11-01T09:52:41.773 回答
0

setOnItemClickListener在您的listItem赞上使用...

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    holder.text.setText("item "+position);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);

holder.text.setOnClickListener(new OnItemClickListener(position));

    return vi;
}


private class OnItemClickListener implements OnClickListener {
        private int mPosition;

        OnItemClickListener(int position) {
            mPosition = position;
        }

        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), mPosition,
                    Toast.LENGTH_LONG);

        }
    }
于 2013-11-01T10:05:06.220 回答