2013-10-25 39 views
0

希望你很好。 我搜索了更多關於如何在getView()方法中實現OnclickListener的方法,但這仍然沒有實現,我想通過單擊位於ListView內的按鈕來執行一個操作。 這是我的代碼。使用SimpleCursorAdapter的OnClickListener的getView()

public class Restaurant extends Activity { 
private DatabaseHandler myDatabaseHandler; 
ListView listContent; 
Context context; 
TextView rest_name; 
TextView rest_hotline; 
ImageButton call; 
ImageView rest_image; 
String image_string; 
String r_n = ""; 
String r_e = ""; 
String r_h = ""; 
private CursorAdapter dataSource; 
RelativeLayout resta; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_restaurant); 
     listContent = (ListView)findViewById(R.id.restaurants_list); 
     call = (ImageButton) findViewById(R.id.call); 

     /* 
     * Create/Open a SQLite database 
     * and fill with dummy content 
     * and close it 
     */ 
     myDatabaseHandler = new DatabaseHandler(this); 
     myDatabaseHandler.openToWrite(); 
     myDatabaseHandler.deleteAll(); 

     myDatabaseHandler.insert("First","1111",R.drawable.ic_launcher); 
     myDatabaseHandler.insert("Second","0000",R.drawable.ic_launcher); 
     myDatabaseHandler.close(); 

     /* 
     * Open the same SQLite database 
     * and read all it's content. 
     */ 
     myDatabaseHandler = new DatabaseHandler(this); 
     myDatabaseHandler.openToRead(); 

     Cursor cursor = myDatabaseHandler.queueAll(); 
     startManagingCursor(cursor); 
     String[] from = new String[]{DatabaseHandler.KEY_REST_NAME,DatabaseHandler.KEY_REST_HOTLINE, DatabaseHandler.KEY_REST_IMAGE}; 
     int[] to = new int[]{R.id.rest_name,R.id.rest_hotline, R.id.rest_image}; 

     final ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.lists, cursor, from, to); 

     listContent.setAdapter(cursorAdapter); 
     myDatabaseHandler.close(); 
} 
private class CustomCursorAdapter extends SimpleCursorAdapter { 

    public CustomCursorAdapter(Context context, int layout, Cursor c, 
      String[] from, int[] to) { 
     super(context, layout, c, from, to); 
     // TODO Auto-generated constructor stub 
    } 
     class ViewHolder { 
     ImageButton b; 
    } 
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
     View view = convertView; 
     ViewHolder holder; 
     if(view == null) { 
      LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = vi.inflate(R.layout.lists, null); 
     holder = new ViewHolder(); 
     holder.b = (ImageButton) view.findViewById(R.id.call); 
     view.setTag(holder); 
     } 
     else 
     { 

      holder = (ViewHolder) view.getTag(); 
     holder.b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show(); 
      } 
     }); 
     } 
     return view;  
    } 

XML文件:列表視圖

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/restau" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:descendantFocusability="blocksDescendants" 
    android:orientation="vertical"> 
    <RelativeLayout 
     android:id="@+id/resta" 
     android:layout_width="fill_parent" 
     android:layout_height="70dip" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:background="#e74c3c"> 
    <ImageView 
     android:id="@+id/rest_image" 
     android:layout_width="70dip" 
     android:layout_height="70dip" 
     android:layout_marginLeft="10dip" 
     android:src="@drawable/ic_launcher"/> 
    <TextView 
     android:id="@+id/rest_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="15dip" 
     android:layout_toRightOf="@+id/rest_image" 
     android:text="ALL" 
     android:textColor="#FFFFFF" 
     android:textStyle="bold" 
     android:textSize="25dip" 
     android:typeface="sans"/> 
    <TextView 
     android:id="@+id/rest_hotline" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="15dip" 
     android:layout_toRightOf="@+id/rest_image" 
     android:text="Hotline" 
     android:layout_below="@+id/rest_name" 
     android:textColor="#FFFFFF" 
     android:textStyle="bold" 
     android:textSize="15dip" 
     android:typeface="sans"/> 
    <ImageButton 
     android:id="@+id/call" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="15dip" 
     android:focusable = "false" 
     android:clickable="true" 
     android:background="@drawable/ic_launcher"/> 
    </RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/stroke" 
     android:layout_width="fill_parent" 
     android:layout_height="1dip" 
     android:layout_below="@+id/cat" 
     android:layout_marginLeft="10dip" 
     android:layout_marginRight="10dip" 
     android:background="#d93b2b"> 
    </RelativeLayout> 
    </RelativeLayout> 
+0

你有什麼錯誤/結果? – Simo

+0

ImageButton在點擊時不起作用,無法執行我需要的操作。 –

回答

0

其行B'coz您不使用CustomCursorAdapter

您正在使用

final ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.lists, cursor, from, to); 

,你給onClick動作裏面CustomCursorAdapter

holder.b.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show(); 
      } 
     }); 

所以,你應該使用像

final CustomCursorAdapter cursorAdapter = new CustomCursorAdapter(this, R.layout.lists, cursor, from, to); 
+0

@AnasSherif爲什麼使用SimpleCursorAdapter?嘗試使用ArrayAdapter或BaseAdapter。您的CustomCursorAdapter實現不完整。 –

+0

它現在有效,但數據應用於行! –

+0

好的,你可以給我一個使用ArrayAdapter或BaseAdapter的例子。 –

0

儘量去除其他statment在getView梅索德

0

我覺得代碼小姐對準使你的麻煩,試試這個方式...希望它能工作..

if(view == null) { 
    LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    view = vi.inflate(R.layout.lists, null); 
    holder = new ViewHolder(); 
    holder.b = (ImageButton) view.findViewById(R.id.call); 
    view.setTag(holder); 
} 
else 
{ 
    holder = (ViewHolder) view.getTag(); 

} 
holder.b.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show(); 
} 
}); 
0

嘗試此

holder.b.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show(); 

       } 

      } 
     }); 

或實現CustomCursorAdapter

private class CustomCursorAdapter extends SimpleCursorAdapter implements OnClickListener 

並使用

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

Toast.makeText(getApplicationContext(), 「ImageClickClicked」,Toast.LENGTH_LONG).show();

} 
相關問題