如何使用SimpleCursorAdapter展开Listview行

问题描述:

我正在使用数据库来充气我的列表视图,并且我正在使用简单的游标适配器来执行此操作。现在我想展开列表项的单击行(如果用户点击行,就可以看到当前行的两个文本视图)。我搜查了很多,但没有找到任何答案。请帮忙!!!!谢谢。如何使用SimpleCursorAdapter展开Listview行

public class CallLogs extends Activity { 
EditText from,to; 
Button call; 
TextView call_from,call_to,date_call,verify; 
private SQLiteDatabase database; 
String fields[] = { "from", "to", "date" }; 
private CursorAdapter dataSource; 
long profile_counts; 
SQLiteDatabase sqLiteDatabase; 
DataBaseHandler dbHelper; 
MyDataBaseHelper databaseHelper; 
MyDBHelper databaseHelper_new; 
Button get; 
ListView listView; 
RelativeLayout r2; 
private SimpleCursorAdapter dataAdapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.call_logs); 

    databaseHelper_new= new MyDBHelper(this); 


    from = (EditText)findViewById(R.id.from); 
    to = (EditText)findViewById(R.id.to); 
    call_from = (TextView)findViewById(R.id.textView4); 
    call_to = (TextView)findViewById(R.id.textView6); 
    date_call = (TextView)findViewById(R.id.textView8); 
    verify = (TextView)findViewById(R.id.call_from); 


    call = (Button) findViewById(R.id.call); 
    get = (Button) findViewById(R.id.get); 

    call.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      String from_num = from.getText().toString(); 

      call_from.setText(from_num); 


      String to_num = to.getText().toString(); 
      call_to.setText(to_num); 


      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); 
      String currentDateandTime = sdf.format(new Date()); 

      DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm:ss"); 
      String date = df.format(Calendar.getInstance().getTime()); 
      date_call.setText(date); 


      Calendar c = Calendar.getInstance(); 
      int seconds = c.get(Calendar.SECOND); 
      Log.d("currentDateandTime",date+""); 

      String duration = "6 seconds"; 

      databaseHelper_new.addFriend(from_num,to_num,duration,date); 
      dataAdapter.notifyDataSetChanged(); 
     } 
    });  
    displayListView(); } 

private void displayListView() { 


    Cursor cursor = databaseHelper_new.getFriends(); 

    // The desired columns to be bound 
    String[] columns = new String[] { "call_from", "call_to","call_duration","call_date"}; 

    // the XML defined views which the data will be bound to 
    int[] to = new int[] { 
      R.id.code, 
      R.id.name, 
      R.id.continent, 
      R.id.region, 
    }; 

    // create the adapter using the cursor pointing to the desired data 
    //as well as the layout information 
    dataAdapter = new MyCursorAdapter(
      this, R.layout.country_info, 
      cursor, 
      columns, 
      to, 
      0); 

    listView = (ListView) findViewById(R.id.listView); 
    // Assign adapter to ListView 
    listView.setAdapter(dataAdapter); 

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> listView, View view, 
           int position, long id) { 
      // Get the cursor, positioned to the corresponding row in the result set 
      Cursor cursor = (Cursor) listView.getItemAtPosition(position); 
      // Get the state's capital from this row in the database. 
      String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("call_from")); 

     Toast.makeText(getApplicationContext(),countryCode, Toast.LENGTH_SHORT).show();}});} 

private class MyCursorAdapter extends SimpleCursorAdapter{ 

    public MyCursorAdapter(Context context, int layout, Cursor c, 
          String[] from, int[] to, int flags) { 
     super(context, layout, c, from, to, flags); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     //get reference to the row 
     View view = super.getView(position, convertView, parent); 
     //check for odd or even to set alternate colors to the row background 
     if(position % 2 == 0){ 
      view.setBackgroundColor(Color.rgb(238, 233, 233)); 
     } 
     else { 
      view.setBackgroundColor(Color.rgb(255, 255, 255)); 
     } 
     return view; 
    }}} 

也许这是不可能的,请使用扩展BaseAdapter

+0

是否有可能在基础适配器设置数据库值类,因为我想光标位置!!!! –

+1

是的,肯定是为游标数据创建模型数组列表并传递给baseadapter类... –

+0

谢谢。我用base adapter完成了。 –