从textview上的列表视图中获取Selected项目,单击android

时间:2013-11-01 09:49:02

标签: android

我有一个listview并使用LayoutInflater概念填充listview。我需要从列表中获取所选项目,每次我点击特定项目时我都会获得最上面的项目。为什么会这样。有没有办法得到我选择的确切项目。我附上了我的代码。

这是我的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>

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

Lazyadaptar课程

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

}

3 个答案:

答案 0 :(得分: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);

    }
}

答案 1 :(得分:1)

使用list.setOnItemClickListener

答案 2 :(得分: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);

        }
    }