2017-03-07 9 views
-2

データベースを使用してリストビューを拡張し、単純なカーソルアダプタを使用しています。今、私はリストアイテムをクリックして行を展開したい(ユーザーが行をクリックした場合、現在行の2つのテキストビューが表示されているように)。私は多くを捜したが、何の答えも見つけられなかった。助けてください!!!!ありがとうございました。SimpleCursorAdapterを使用してリストビューの行を展開する方法

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

答えて

1

は、多分それは私がカーソル位置にしたいので、BaseAdapter

+0

を拡張し、ベースアダプタで、データベースの値を設定することが可能となるクラスを使用してくださいことはできません!!!! –

+1

はい、間違いなくカーソルデータのモデル配列リストを作成し、baseadapterクラスに渡してください... –

+0

ありがとうございます。私はベースアダプターでそれをやった。 –

関連する問題