onClick不适用于自定义列表视图

时间:2011-07-28 23:56:35

标签: android listview onclick

Test.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;
}

}}

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

2 个答案:

答案 0 :(得分:1)

我假设listview.xml是用于单个列表视图项的布局?你有可聚焦和可点击的对象。至于TextView元素,您可以使它们“不可聚焦”。阅读:here

答案 1 :(得分:0)

你试过吗,

删除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是否有错误。