android - onClick 不适用于自定义 ListView

标签 android listview onclick

测试.java:

public class Test extends Activity{
/** Called when the activity is first created. */
protected Dialog mSplashDialog;
private static final String[] country = { "Iceland", "India", "Indonesia","Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia","Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania","Luxembourg" };
private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD","EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD","LTL ", "EUR"};
ListView l1;
private OnItemClickListener listener;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance();
    if (data != null) {
        // Show splash screen if still loading
        if (data.showSplashScreen) {
            showSplashScreen();
        }
        setContentView(R.layout.main);        

        // Rebuild your UI with your saved state here
    } else { 
        showSplashScreen();
        setContentView(R.layout.main);
        final ListView l1 = (ListView) findViewById(android.R.id.list);
        l1.setAdapter(new EfficientAdapter(this)); 
        l1.setItemsCanFocus(true);
        l1.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
                  Object listItem = l1.getItemAtPosition(position);
                  Log.v("MyApp", "get onItem Click position= " + position);
                  Toast.makeText(getApplicationContext(), listItem.toString(),Toast.LENGTH_SHORT).show();
                  Toast.makeText(getApplicationContext(), "Valid", Toast.LENGTH_SHORT).show();
               } 
            });
    }
}



@Override
public Object onRetainNonConfigurationInstance() {
    MyStateSaver data = new MyStateSaver();
    // Save your important data here

    if (mSplashDialog != null) {
        data.showSplashScreen = true;
        removeSplashScreen();
    }
    return data;
}

/**
 * Removes the Dialog that displays the splash screen
 */
protected void removeSplashScreen() {
    if (mSplashDialog != null) {
        mSplashDialog.dismiss();
        mSplashDialog = null;
    }
}

/**
 * Shows the splash screen over the full Activity
 */
protected void showSplashScreen() {
    mSplashDialog = new Dialog(this, R.style.SplashScreen);
    mSplashDialog.setContentView(R.layout.splashscreen);
    mSplashDialog.setCancelable(false);
    mSplashDialog.show();

    // Set Runnable to remove splash screen just in case
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        removeSplashScreen();
      }
    }, 3000);
}

/**
 * Simple class for storing important data across config changes
 */
private class MyStateSaver {
    public boolean showSplashScreen = false;
    // Your other important fields here
}
public static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

public EfficientAdapter(Context context) {
    mInflater = LayoutInflater.from(context);   
}
public int getCount() {
    return country.length;
    }
public Object getItem(int position) {
    return position;
    }
public long getItemId(int position) {
    return position;
    }
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listview, null);
        convertView.setClickable(true);
        convertView.setFocusableInTouchMode(true);
        convertView.setFocusable(true);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.TextView01);
        holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
        holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
        holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(curr[position]);
    holder.text2.setText(country[position]);
    holder.text3.setText(country[position]);
    holder.text4.setText(country[position]);

    return convertView;
    }
    static class ViewHolder {
        TextView text4;
        TextView text;
        TextView text2;
        TextView text3;
}

}}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/theater">

<ListView 
    android:id="@android:id/list"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:listSelector="@android:color/transparent" 
    android:cacheColorHint="@android:color/transparent"
    android:layout_below = "@+id/catagory"/>
<Button 
    android:id="@+id/GPS"
    android:background="@drawable/fire" 
    android:layout_marginTop="20px"
    android:layout_height="70px" 
    android:layout_width="80px" 
    android:layout_alignParentLeft="true"/>
<EditText 
    android:id="@+id/search"  
    android:layout_width="300px"
    android:layout_height="70px"
    android:layout_marginTop="20px"
    android:hint="Search theater"
    android:textSize="30px"
    android:inputType="text"
    android:layout_toRightOf="@+id/GPS"/>

<Button 
    android:text="OK" 
    android:layout_width="100px" 
    android:layout_height="70px"
    android:id="@+id/submit" 
    android:layout_marginTop="20px" 
    android:layout_toRightOf="@+id/search"/>

<TextView
    android:id="@+id/catagory"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Recent Movie Theaters"
    android:layout_below="@id/search"
    android:gravity="center" 
    android:layout_centerInParent="true" 
    android:textStyle="bold"/>
</RelativeLayout>

ListView .xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" 
android:gravity="left|center"
android:layout_width="wrap_content" 
android:paddingBottom="5px"
android:paddingTop="5px"
android:paddingLeft="5px">

<TextView 
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:gravity="center"
android:background="@drawable/icon" 
android:textColor="#FFFF00"
android:text="hi"></TextView>

<TextView 
android:text="@+id/TextView02" 
android:id="@+id/TextView02"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:textStyle="bold"
android:textSize="25px"
android:textColor="#0099CC"
android:layout_toRightOf="@+id/TextView01"></TextView>

<TextView 
android:text="@+id/TextView03" 
android:id="@+id/TextView03"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:textColor="#0099CC"
android:layout_toRightOf="@+id/TextView01"
android:layout_below="@+id/TextView02"></TextView>

<TextView 
android:text="@+id/TextView04" 
android:id="@+id/TextView04"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_marginLeft="10px" 
android:textColor="#0099CC"
android:layout_toRightOf="@+id/TextView01"
android:layout_below="@+id/TextView03"></TextView>

<Button 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
android:gravity="right" 
android:id="@+id/button" 
android:layout_width="wrap_content"
android:text="Bye"
android:layout_alignParentRight="true"
android:layout_marginRight="10px">
</Button>

</RelativeLayout>

最佳答案

你试过吗

删除 l1.setItemsCanFocus(true); 并简单地使用:

...
else { 
        showSplashScreen();
        setContentView(R.layout.main);
        final ListView l1 = (ListView) findViewById(android.R.id.list);
        l1.setAdapter(new EfficientAdapter(this));         
        l1.setOnItemClickListener(new OnItemClickListener() {
               @Override
               public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
                  Object listItem = l1.getItemAtPosition(position);
                  Log.v("MyApp", "get onItem Click position= " + position);
                  Toast.makeText(getApplicationContext(), listItem.toString(),Toast.LENGTH_SHORT).show();
                  Toast.makeText(getApplicationContext(), "Valid", Toast.LENGTH_SHORT).show();
               } 
            });
    }

和删除

convertView.setClickable(true);
convertView.setFocusableInTouchMode(true);
convertView.setFocusable(true);

并像这样使用它:

public static class EfficientAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    public EfficientAdapter(Context context) {
        mInflater = LayoutInflater.from(context);   
    }
    public int getCount() {
        return country.length;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
           convertView = mInflater.inflate(R.layout.listview, null);

           holder = new ViewHolder();
           holder.text = (TextView) convertView.findViewById(R.id.TextView01);
           holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
           holder.text3 = (TextView) convertView.findViewById(R.id.TextView03);
           holder.text4 = (TextView) convertView.findViewById(R.id.TextView04);

           convertView.setTag(holder);
       } 
       else {
          holder = (ViewHolder) convertView.getTag();
       }

       holder.text.setText(curr[position]);
       holder.text2.setText(country[position]);
       holder.text3.setText(country[position]);
       holder.text4.setText(country[position]);

       return convertView;
    }
    static class ViewHolder {
        TextView text4;
        TextView text;
        TextView text2;
        TextView text3;
    }
}

??

如果您尝试过此操作,但它不起作用,请向我们展示您的 logcat 错误(如果您遇到任何错误)。

关于android - onClick 不适用于自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867171/

相关文章:

android - 触摸更改 TextView 背景颜色

android - ListView 动画淡出然后添加新行

.net - Listview 列标题不显示 VB.Net

android - 自动点击提交按钮以及如何通过提交按钮的时间限制?

java - 为什么我无法读取 sd 中的文本文件?

android - 如何确定地理坐标附近是否存在街道或地址?

android - 使用自定义项目 View 从 ListView 中获取项目

java - ArrayList空指针异常

javascript - 使用className.onclick JS调用函数

javascript - 调用其他标签的onclick事件