通过单击自定义列表视图中包含3个字段/列的项目,从sqlite检索数据以执行编辑/删除

时间:2012-12-01 12:47:24

标签: android listview

先生,您可以通过点击相应的自定义列表视图项来帮助我如何显示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;
}

1 个答案:

答案 0 :(得分:0)

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