0

先生,您能帮我了解如何通过单击相应的自定义列表视图项来显示 sqlite 的详细信息吗?我的列表视图包含三列,所以我希望它显示在另一个活动中,我可以在其中编辑信息和删除等。起初,我想使用行 ID,但放弃了这个想法,因为如果我试图删除一个项目在数据库中,我无法使用已删除的行 ID。所以listview和数据库的行ID不匹配。例如。数据库 1,2,3 中的行 ID ...如果我删除了第 2 行,如果我添加另一个项目,它只会添加到第 4 行...就像第 1,3,4 行一样。提前感谢您的帮助

public class CustomListView extends Activity {
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    GroupDb info = new GroupDb(this);
    info.open();
    ArrayList<Contact> searchResults = info.getView();


    final ListView lv = (ListView) findViewById(R.id.srListView);
    lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
    info.close();

    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
}

这是我的 custombaseadapter

public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Contact> searchArrayList;

private LayoutInflater mInflater;

public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) {
    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
}

public int getCount() {
    return searchArrayList.size();
}

public Object getItem(int position) {
    return searchArrayList.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_row_view, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.name);

        holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

        holder.status = (TextView) convertView.findViewById(R.id.status);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtName.setText(searchArrayList.get(position).getName());

    holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber());

    holder.status.setText(searchArrayList.get(position).getStatus());

    return convertView;
}

static class ViewHolder {
    TextView txtName;
    TextView txtPhone;
    TextView status;
}
}

这是我在自定义列表视图中使用 GroupDb.class 中的三个字段/列保存数据库值的方法

public ArrayList<Contact> getView() 
{
    // TODO Auto-generated method stub
    ArrayList<Contact> results = new ArrayList<Contact>();
    String[] columns = new String[]{KEY_NAME, KEY_NUMBER, KEY_STATUS};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String sName = "";
    String nName = "";
    String status = "";
    int iName = c.getColumnIndex(KEY_NAME);
    int iNumber = c.getColumnIndex(KEY_NUMBER);
    int iStatus = c.getColumnIndex(KEY_STATUS);
    Contact contact;
    for(c.moveToFirst(); ! c.isAfterLast(); c.moveToNext())
    {
        contact = new Contact();
        sName += c.getString(iName);
        nName += c.getString(iNumber);
        status += c.getString(iStatus);
        contact.setName(sName);
        contact.setPhoneNumber(nName);
        contact.setStatus(status);
        results.add(contact);
        sName = "";
        nName = "";
        status = "";
    }
    return results;
}
4

1 回答 1

0

您正在维护联系人数组列表。单击列表视图时,从您的数组列表中的给定位置获取位置获取联系人对象。您必须在您的联系人类中保存主键列。

于 2012-12-01T12:50:39.050 回答