2012-11-04 166 views
1

我有一個自定義的ListView,其中每一行由其中的許多TextViews組成。當我點擊ListItem時,OnListItemClick()不會被調用。那麼你如何獲得自定義列表視圖的選擇?如何從自定義ListView中獲取選定的項目?

感謝您的幫助!

XML爲行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/priorityView" 
     android:layout_width="20dip" 
     android:layout_height="match_parent" 
     android:background="#cccccc" 
     android:layout_marginTop="2dip" 
     android:layout_marginBottom="2dip" /> 


    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:weightSum="1.0" 
     > 
     <TextView 
     android:id="@+id/dateText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:paddingBottom="2dip" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" 
     android:paddingTop="2dip" 
     android:text="@string/empty" 
     android:textSize="20dip" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/monthText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:padding="5dip" 
     android:text="@string/empty" 
     android:textSize="10dip" > 
    </TextView> 

    </LinearLayout> 



    <TextView 
     android:id="@+id/timeText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:padding="5dip" 
     android:text="@string/empty" > 
    </TextView> 

    <TextView 
     android:id="@+id/titleText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:padding="5dip" 
     android:text="@string/empty" 
     android:textStyle="bold" 
     android:layout_weight="1.0" > 
    </TextView> 

    <CheckBox 
     android:id="@+id/selectedCheckbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:padding="5dip" > 
    </CheckBox> 

</LinearLayout> 

XML爲ListView控件:

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

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" > 
    </ListView> 


</RelativeLayout> 

的Java

public class MainActivity extends ListActivity { 


    private AppointmentsDB dbHelper; 
    private Cursor cursor; 
    private AppointmentsCursorAdapter cursorAdapter; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     dbHelper = new AppointmentsDB(this); 

     StringBuilder selectQuery = new StringBuilder(); 
     selectQuery.append("SELECT "+AppointmentsDB.KEY_ID+","); 
     selectQuery.append(AppointmentsDB.KEY_TITLE + ", "); 
     selectQuery.append(AppointmentsDB.KEY_DESCRIPTION + ", "); 
     selectQuery.append(AppointmentsDB.KEY_PRIORITY + ", "); 
     selectQuery.append(AppointmentsDB.KEY_DATE_TIME + ", "); 
     selectQuery.append(AppointmentsDB.KEY_DURATION + ", "); 
     selectQuery.append(AppointmentsDB.KEY_ALARM_TIME + " FROM " 
       + AppointmentsDB.DATABASE_TABLE + " "); 
     selectQuery.append("ORDER BY " + AppointmentsDB.KEY_DATE_TIME + " ASC"); 

     cursor = dbHelper.openReadableDatabase().rawQuery(
       selectQuery.toString(), null); 

     String[] columnNames = new String[] { }; 
     int[] ids = new int[] { }; 

     cursorAdapter = new AppointmentsCursorAdapter(this, 
       R.layout.row, cursor, columnNames, ids); 
     this.setListAdapter(cursorAdapter); 
    } 

    // This class sets our customised layout for the ListView 
    class AppointmentsCursorAdapter extends SimpleCursorAdapter { 

     private int layout; 
     private int[] colours; 

     @SuppressWarnings("deprecation") 
     public AppointmentsCursorAdapter(Context context, int layout, Cursor c, 
       String[] from, int[] to) { 
      super(context, layout, c, from, to); 


      this.layout = layout; 
      colours = new int[] { 
        context.getResources().getColor(R.color.light_gray), 
        context.getResources().getColor(R.color.green), 
        context.getResources().getColor(R.color.orange), 
        context.getResources().getColor(R.color.brick_red) }; 
     } 

     public View newView(Context context, Cursor cursor, ViewGroup parent) { 
      LayoutInflater inflater = LayoutInflater.from(context); 
      View view = inflater.inflate(layout, parent, false); 

      TextView titleText = (TextView) view.findViewById(R.id.titleText); 
      TextView priorityView = (TextView) view 
        .findViewById(R.id.priorityView); 
      TextView dateText = (TextView) view.findViewById(R.id.dateText); 
      TextView monthText = (TextView) view.findViewById(R.id.monthText); 
      TextView timeText = (TextView) view.findViewById(R.id.timeText); 

      String title = cursor.getString(cursor 
        .getColumnIndex(AppointmentsDB.KEY_TITLE)); 

      int priority = 0; 
      String _priority = cursor.getString(cursor.getColumnIndex(AppointmentsDB.KEY_PRIORITY)); 
      if(_priority != null) 
       priority = Integer.parseInt(_priority); 

      long dateTime = Long.parseLong(cursor.getString(cursor 
        .getColumnIndex(AppointmentsDB.KEY_DATE_TIME))); 

      Calendar calendar = new GregorianCalendar(); 
      calendar.setTimeInMillis(dateTime); 

      SimpleDateFormat timeFormat = new SimpleDateFormat(
        "dd,MMM,HH:mm aaa"); 
      String[] tokens = timeFormat.format(calendar.getTime()).split(","); 
      dateText.setText(tokens[0]); 
      monthText.setText(tokens[1]); 
      timeText.setText(tokens[2]); 

      titleText.setText(title); 
      priorityView.setBackgroundColor(colours[priority]); 

      return view; 
     } 

     @Override 
     public void bindView(View view, Context context, Cursor cursor) { 
      super.bindView(view, context, cursor); 

      TextView titleText = (TextView) view.findViewById(R.id.titleText); 
      TextView priorityView = (TextView) view 
        .findViewById(R.id.priorityView); 
      TextView dateText = (TextView) view.findViewById(R.id.dateText); 
      TextView monthText = (TextView) view.findViewById(R.id.monthText); 
      TextView timeText = (TextView) view.findViewById(R.id.timeText); 

      String title = cursor.getString(cursor 
        .getColumnIndex(AppointmentsDB.KEY_TITLE)); 
      int priority =0; 
      String _priority = cursor.getString(cursor 
        .getColumnIndex(AppointmentsDB.KEY_PRIORITY)); 
      if(_priority != null) 
       priority = Integer.parseInt(_priority); 


      long dateTime = Long.parseLong(cursor.getString(cursor 
        .getColumnIndex(AppointmentsDB.KEY_DATE_TIME))); 

      Calendar calendar = new GregorianCalendar(); 
      calendar.setTimeInMillis(dateTime); 

      SimpleDateFormat timeFormat = new SimpleDateFormat(
        "dd,MMM,HH:mm aaa"); 
      String[] tokens = timeFormat.format(calendar.getTime()).split(","); 
      dateText.setText(tokens[0]); 
      monthText.setText(tokens[1]); 
      timeText.setText(tokens[2]); 

      titleText.setText(title); 
      priorityView.setBackgroundColor(colours[priority]); 

     } 
    } 
} 
+0

把佈局文件從列表和列表項兩者。 – rgrocha

+0

請顯示您的java代碼.. –

回答

1

我找到解決方案。

您必須設置android:focusable屬性false自定義ListView,包括佈局控件內的每個部件。

+0

如果你想downvote,請提供一個理由! –

0

嘗試使用OnItemClick(),而不是OnListItemClick()

這是通過發送光標

 SimpleCursorAdapter yearadapter = 
      new SimpleCursorAdapter(this, R.layout.listview, yearcursor, yearfrom, yearto); 
     setListAdapter(yearadapter); 
     amount.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, 
        long id) { 

        //Your code when item get clicked 
       } 
      }); 

它應該由SimpleCursorAdapter創建自定義列表視圖。

+0

我的類的子類ListView。沒有OnItemClick方法可以覆蓋。 –

+0

所以你應該有setOnItemClickListener()方法,使用它來註冊一個新的OnItemClickListener,並在onItemClick()方法中點擊一個項目時放置任何你想做的事情,就像Venture寫的一樣。 – Flawyte

+0

對不起,我的意思是它的子類ListActivity。我無法重寫該方法。 –

0

下面的代碼會給你的列表視圖項:

private ListView listView = (ListView) findviewbyid(.........); 

     listView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      View v = v.getChildAt(arg2);//This will give the listview item 


     } 
    }); 
相關問題