2013-11-01 100 views
1

我有一個列表視圖並使用LayoutInflater概念填充列表視圖。我需要從列表中獲取選定的項目,每當我點擊特定的項目時,我會得到最上面的項目。它爲什麼會發生。有什麼方法可以獲得我選擇的確切項目嗎?我附上了我的代碼。從textview的列表視圖中獲取所選項目單擊Android

這是我的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; 
} 

}

回答

1

使用list.setOnItemClickListener

+0

我需要點擊對XML的一個TextView。那麼它有可能如何? –

+0

如果您需要在列表元素上進行「子點擊」,最簡單的方法是重寫列表適配器的getView,並設置列表元素上的單擊監聽器;然而在你的例子中,你只有一個圖像和文本,通常它更容易讓一切都可點擊 – user517708

0

使用setOnItemClickListenerlistItem像...

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

     } 
    } 
+0

我得到了錯誤「類型View中的方法setOnClickListener(View.OnClickListener)不適用於參數(LazyAdapter.OnItemClickListener )「on the holder holder.text.setOnClickListener(new OnItemClickListener(position)); –

+0

@Deepu你使用相同的代碼嗎? –

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

    } 
} 
相關問題