如何使用SimpleCursorAdapter扩展Listview行

时间:2017-03-07 11:05:51

标签: android listview 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 个答案:

答案 0 :(得分:1)

也许不可能请使用扩展 BaseAdapter

的类