如何在android中的列表视图中检索单个数据

时间:2017-04-10 21:47:07

标签: android

我使用内置列表视图以列表格式显示数据库中的数据,该列表格式显示同一行中的名称和联系号码。我想修改名称或数字。所以如何同时获取名称和号码在自定义列表视图中单独显示并显示在其他一些文本字段中。 我尝试使用onTouchListener,但这会返回名称和数字。

我的代码以列表的形式显示姓名和联系电话:

package com.example.hp.panic;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle; 
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class Modify extends Activity {

    SQLiteDatabase database;
    ListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        try {
            database = openOrCreateDatabase("myDB", Context.MODE_PRIVATE, 
null);

            listView = (ListView) findViewById(R.id.contact_list);

            ArrayList<String> arrayList = new ArrayList<>();

            Cursor c = database.rawQuery("SELECT * FROM contacts ", null);

            if (c.getCount() > 0) {
                while (c.moveToNext()) {
                    String data = c.getString(0) + " " + c.getString(1);
                    arrayList.add(data);
                    ListAdapter listAdapter = new ArrayAdapter<>(this, 
    android.R.layout.simple_list_item_1, arrayList);
                    listView.setAdapter(listAdapter);
                }
            } else {
                Toast.makeText(Modify.this, "Table Empty", 
    Toast.LENGTH_SHORT).show();
            }
            c.close();

            listView.setOnItemClickListener(new 
    AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View 
    view,int i, long l) {
                     switch (i) {
                        case 0: {

                        }
                        break;
                        case 1: {
                            //Intent intent = new 
   // Intent(Modify.this,Modify2.class);
                                //startActivity(intent);
                            }
                            break;
                            case 2: {
                                //Intent intent = new 
    //Intent(Modify.this,Modify2.class);
                                //startActivity(intent);
                            }
                            break;
                             case 3: {
                                //Intent intent = new Intent(Modify.this, 
       //Modify2.class);  
                                //startActivity(intent);
                            }
                             break;
                             case 4: {
                              /*  Intent intent = new Intent(Modify.this, 
        Modify2.class);
                                startActivity(intent);*/
                            }
                            break;
                        }
                     }
                 });
             } catch (Exception e) {
                 Log.d("Error", String.valueOf(e));
             }
         }
     }

0 个答案:

没有答案