简体   繁体   中英

Android custom listview not clickable on item select

I am working with custom listView in android. but I cant able click the item in listview.

my code is

Adapter code(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 file goes like this

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

Thanks in advance

Regards, Sathish

I had the same problem and I solved by add this code to getView() method in my custom adapter class, yours BankArrayListAdapter.java

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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