Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用SimpleCursorAdapter展开Listview行_Android_Listview_Simplecursoradapter - Fatal编程技术网

Android 如何使用SimpleCursorAdapter展开Listview行

Android 如何使用SimpleCursorAdapter展开Listview行,android,listview,simplecursoradapter,Android,Listview,Simplecursoradapter,我正在使用数据库来膨胀我的列表视图,我正在使用简单的游标适配器来实现这一点。现在,我想展开列表项单击上的行(就像用户单击行一样,当前行的两个文本视图对他可见)。我搜索了很多,但没有找到任何答案。请帮忙!!!!多谢各位 public class CallLogs extends Activity { EditText from,to; Button call; TextView call_from,call_to,date_call,verify; private SQLiteDatabase d

我正在使用数据库来膨胀我的列表视图,我正在使用简单的游标适配器来实现这一点。现在,我想展开列表项单击上的行(就像用户单击行一样,当前行的两个文本视图对他可见)。我搜索了很多,但没有找到任何答案。请帮忙!!!!多谢各位

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;
    }}}
公共类调用日志扩展活动{
编辑文本从,到;
按钮呼叫;
text查看呼叫来源、呼叫目的地、日期、验证;
专用数据库;
字符串字段[]={“从”、“到”、“日期”};
专用游标适配器数据源;
长剖面计数;
SQLiteDatabase SQLiteDatabase;
数据库处理程序dbHelper;
MyDataBaseHelper databaseHelper;
MyDBHelper数据库Helper_新建;
按钮获取;
列表视图列表视图;
相对寿命r2;
私有SimpleCursorAdapter数据适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.call_日志);
databaseHelper_new=新的MyDBHelper(this);
from=(EditText)findViewById(R.id.from);
to=(EditText)findViewById(R.id.to);
调用_from=(TextView)findViewById(R.id.textView4);
调用_to=(TextView)findViewById(R.id.textView6);
date_call=(TextView)findViewById(R.id.textView8);
验证=(TextView)findViewById(R.id.call\u from);
call=(按钮)findViewById(R.id.call);
get=(按钮)findviewbyd(R.id.get);
call.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
字符串from_num=from.getText().toString();
从.setText调用_(从_num);
字符串to_num=to.getText().toString();
调用_to.setText(to_num);
SimpleDataFormat sdf=新的SimpleDataFormat(“yyyyMMdd_HHmmss”);
字符串currentDateandTime=sdf.format(new Date());
DateFormat df=新的简化格式(“EEE,d MMM yyyy,HH:mm:ss”);
字符串date=df.format(Calendar.getInstance().getTime());
date_call.setText(日期);
Calendar c=Calendar.getInstance();
int seconds=c.get(日历秒);
Log.d(“currentDateandTime”,日期+”);
字符串持续时间=“6秒”;
databaseHelper_new.addFriend(from_num,to_num,duration,date);
dataAdapter.notifyDataSetChanged();
}
});     
displayListView();}
私有void displayListView(){
Cursor Cursor=databaseHelper_new.getFriends();
//要绑定的所需列
String[]columns=新字符串[]{“call\u from”、“call\u to”、“call\u duration”、“call\u date”};
//数据将绑定到的XML定义的视图
int[]至=新int[]{
R.id.代码,
R.id.name,
R.id.大陆,
R.id.区域,
};
//使用指向所需数据的光标创建适配器
//以及布局信息
dataAdapter=新的MyCursorAdapter(
这是R.layout.country_信息,
光标,
柱,
到
0);
listView=(listView)findViewById(R.id.listView);
//将适配器分配给ListView
setAdapter(dataAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView列表视图、视图、,
内部位置,长id){
//获取光标,定位到结果集中的对应行
游标游标=(游标)listView.getItemAtPosition(位置);
//从数据库中的此行获取州的资本。
String countryCode=cursor.getString(cursor.getColumnIndexOrThrow(“call_from”);
Toast.makeText(getApplicationContext(),countryCode,Toast.LENGTH_SHORT.show();}});}
私有类MyCursorAdapter扩展了SimpleCursorAdapter{
公共MyCursorAdapter(上下文上下文、int布局、游标c、,
字符串[]从,int[]到,int标志){
super(上下文、布局、c、from、to、标志);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//获取对该行的引用
视图=super.getView(位置、转换视图、父级);
//检查奇数或偶数以设置行背景的备用颜色
如果(位置%2==0){
视图.setBackgroundColor(Color.rgb(238233233));
}
否则{
view.setBackgroundColor(Color.rgb(255、255、255));
}
返回视图;
}}}

可能不可能请使用扩展了BaseAdapter的类

是否可以在BaseAdapter中设置数据库值,因为我希望光标位置为!!!!是的,为光标数据创建模型数组列表并传递给baseadapter类…如果您实现了目标,请右勾@苏米特库马尔