2011-07-28 50 views
1

Test.java:的onClick不使用自定義列表視圖工作

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

你或許應該解釋究竟發生了什麼問題,否則人們將無法幫助你。 – Joost

回答

1

我認爲listview.xml是您用於單個列表視圖項目的佈局?那裏有可聚焦和可點擊的對象。至於TextView元素,例如,可以讓它們「不可聚焦」。閱讀:here

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錯誤,如果您有任何問題。