2017-03-07 9 views
-2

데이터베이스를 사용하여 목록보기를 부 풀리고 간단한 커서 어댑터를 사용하고 있습니다. 이제는 목록 항목을 클릭하여 행을 확장하려고합니다 (사용자가 행을 클릭하면 현재 행의 두 텍스트보기가 표시됨). 나는 많은 것을 찾았지만 어떤 답을 찾지 못했습니다. 도와주세요!!!! 고맙습니다.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; 
    }}} 

답변

1

은 아마 내가 커서 위치를 원하기 때문에, BaseAdapter

+0

를 확장은 기본 어댑터를 데이터베이스 값을 설정할 수 있습니다 클래스를 사용하시기 바랍니다 수는 없습니다 !!!! –

+1

예 커서 데이터에 대한 모델 배열 목록을 작성하고 baseadapter 클래스에 전달하십시오. –

+0

감사합니다. 나는베이스 어댑터로 해냈다. –