简体   繁体   English

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

[英]Get the Selected item from a list view on textview click in android

I have a listview and fill the listview using LayoutInflater concept. 我有一个listview并使用LayoutInflater概念填充listview。 I need to get the selected item from the list, every time i click on a particular item i get the topmost item. 我需要从列表中获取所选项目,每次我点击特定项目时我都会获得最上面的项目。 Why does it occur. 为什么会这样。 Is there any way to get the exact item which i selected. 有没有办法得到我选择的确切项目。 i attached my code with this. 我附上了我的代码。

this is my xml files main.xml 这是我的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 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>

and the class file is 而类文件是

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

} }

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

    }
}

使用list.setOnItemClickListener

Use setOnItemClickListener on your listItem Like... listItem上使用setOnItemClickListener Like ...

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

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM