0

我正在使用android中的自定義listView。但我無法在列表視圖中單擊該項目。Android自定義列表視圖不可點擊項目選擇

我的代碼是

適配器代碼(BankArrayListAdapter.java)

package com.example.customlist; 

import java.util.List; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.text.method.LinkMovementMethod; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class BankArrayListAdapter extends ArrayAdapter<Bank>{ 

    private int resource; 
    private LayoutInflater inflater; 
    private Context ctx; 

    public BankArrayListAdapter(Context context, int resourceId, List<com.example.customlist.Bank> ls) 



    { 
     super(context, resourceId, ls); 

     resource = resourceId; 

     inflater = LayoutInflater.from(context); 
     ctx=context; 

     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 


      /* create a new view of my layout and inflate it in the row */ 
     convertView = inflater.inflate(resource, null); 

     /* Extract the city's object to show */ 
     Bank bank = (Bank) getItem(position); 

     /* Take the TextView from layout and set the city's name */ 
     TextView txtName = (TextView) convertView.findViewById(R.id.textView1); 
     txtName.setText(bank.getName()); 

     /* Take the TextView from layout and set the city's wiki link */ 
     TextView txtWiki = (TextView) convertView.findViewById(R.id.textView2); 
     txtWiki.setTextSize(13); 
     txtWiki.setMovementMethod(LinkMovementMethod.getInstance()); 
     txtWiki.setText(bank.getUrl()); 

     /* Take the ImageView from layout and set the city's image */ 
     ImageView imageCity = (ImageView) convertView.findViewById(R.id.imageView1); 
     String uri = "drawable/" + bank.getLogo(); 
     int imageResource = ctx.getResources().getIdentifier(uri, null, ctx.getPackageName()); 
     Drawable image = ctx.getResources().getDrawable(imageResource); 
     imageCity.setImageDrawable(image); 


     return convertView; 
    } 



} 

MainActivity.java

package com.example.customlist; 



import java.util.ArrayList; 
import java.util.List; 



import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

public class Main extends Activity { 


    ListView lv; 
    List<Bank> ls; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     lv= (ListView) findViewById(R.id.listView1); 


     ls= new ArrayList<Bank>(); 



     ls.add(new Bank("sbi","State Bank Of India","http://www.sbi.com")); 


     ls.add(new Bank("iob", "India Overseas Bank","http://www.iob.com")); 

     ls.add(new Bank("icici","ICICI","http://www.icici.com")); 




     //lv.setAdapter(new BankArraylistA); 
     lv.setAdapter(new BankArrayListAdapter(Main.this, R.layout.banklist, ls)); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
       public void onItemClick(AdapterView<?> a, View v, int position, long id) { 

        Toast.makeText(Main.this,"Your Listener Works!",Toast.LENGTH_SHORT).show(); 
        // System.out.println("Name: "+ls.get(position).getName()); 
        // String s =(String) ((TextView) v.findViewById(R.id.From)).getText(); 
        // Toast.makeText(Messages.this, s, Toast.LENGTH_LONG).show(); 
       } 
     }); 


     System.out.println(ls); 




    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

activity_main.xml中的文件是這樣的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:descendantFocusability="blocksDescendants" 
    tools:context=".Main" > 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
    </ListView> 

</LinearLayout> 

在此先感謝

問候, Sathish所在

+0

你有沒有嘗試過擴展ListActivity和onlistitemclick()? – 2013-04-05 05:32:29

+0

嘗試刪除此屬性android:descendantFocusability =「blocksDescendants」 – Pragnani 2013-04-05 05:34:34

+1

我認爲你的LinkMovementMethod是問題原因。 – Newts 2013-04-05 05:37:38

回答

0

我有同樣的問題,我解決了添加此代碼getView()方法,在我的自定義適配器類,你BankArrayListAdapter.java

convertView.setOnClickListener(new OnClickListener() 
{ 
     @Override 
     public void onClick(View v) 
     { 
      Log.v("LV_ITEM", "Position of item is " + position); 
     } 
}); 
相關問題