如何通过从数据库获取数据在Listview中显示数据

时间:2014-05-08 08:56:49

标签: android android-listview custom-adapter

我是一名新的Android开发人员,我希望在列表视图中显示数据,数据来自db,我正在使用ArrayAdapter,但没有显示任何人可以帮助我......

我的主要活动代码在这里

 public class SmsMerge extends Activity  {
        private ImageButton btn;
        private ImageButton imgb;
        private TemplateOperation templateDBoperation;
        ArrayList<List> arr;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sms_merge);
            templateDBoperation = new TemplateOperation(this);
            templateDBoperation.open();
            List values = templateDBoperation.getAllTemplate();
            arr=new ArrayList<List>();
               arr.add((ArrayList<List>)values);
            ListView listview = (ListView)findViewById(android.R.id.list);
    MyListAdapter adpttt=new MyListAdapter(SmsMerge.this,R.layout.row,arr);
            listview.setAdapter(adpttt);

    }
    }

CustomAdapter Code就在这里

package com.example.sqliteapplication;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends ArrayAdapter<List> {
    List names;
    int inflatr;
    Context ctxt;
    ArrayList<List> data=new ArrayList<List>();
    public MyListAdapter(Context context, int layoutResourceId,
            ArrayList<List> data) {
        super(context, layoutResourceId, data);
        this.inflatr = layoutResourceId;
        this.ctxt = context;
        this.data= data;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserHolder holder = null;
         View row = convertView;
        if(convertView==null)
        {
             LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
              row = inflater.inflate(inflatr, parent, false);
             holder = new UserHolder();
             holder.btnEdit = (Button) row.findViewById(R.id.atomPay_removePay);
             row.setTag(holder);
        }
        else
        {
             holder = (UserHolder) row.getTag();            
        }
        List dta=data.get(position);
        holder.btnEdit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee", Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }


    static class UserHolder {
        TextView textName;
        TextView textAddress;
        TextView textLocation;
        Button btnEdit;
        Button btnDelete;
        }



}

3 个答案:

答案 0 :(得分:2)

首先纠正这个

return row;

而不是

return convertView;

在您的adapter getView(....)方法中,并发布您的所有logcat并修改@Override getCount(),因为@Biraj说

在更正的代码下方

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    UserHolder holder = null;
     View row = null;
    if(convertView==null)
    {
         LayoutInflater inflater = ((Activity) ctxt).getLayoutInflater();
         row = inflater.inflate(R.layout.row, parent, false);
         holder = new UserHolder();
         holder.btnEdit = (Button) row.findViewById(R.id.atomPay_removePay);
         row.setTag(holder);
    }
    else
    {
         holder = (UserHolder) row.getTag();            
    }
    List dta=data.get(position);
    holder.btnEdit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
             Toast.makeText(ctxt, "Humayoon    Siddiqueeeeeeeeeeeeeeeeeee", Toast.LENGTH_SHORT).show();
        }
    });
    return row;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return data.size();
}

答案 1 :(得分:1)

AS @Simple Plan说

首先纠正这个

return row;

而不是

return convertView;

同时修改此

@Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

答案 2 :(得分:0)

 @Override
    public int getCount() {
        return data.size();
    } 

 @Override
        public View getView(int position, View mView, ViewGroup parent) {
            mCursor = getCursor();
            mCursor.moveToPosition(position);
            if (mView == null) {
                mView = mInflater.inflate(R.layout.listitem_create_edit_def, null);
                mViewHolder = new ViewHolder();

                //intializes view in a view holder
                tvssfg = (TextView)mView.findViewById(R.id.tv_process_name);
                btnsfdg = (Button) mView.findViewById(R.id.btn_add_steps);
                btntrey = (Button) mView.findViewById(R.id.btn_add_tags);
                checkProcess = (CheckBox) mView.findViewById(R.id.cb_process);


                mView.setTag(mViewHolder);
            } 
            else
                mViewHolder = (ViewHolder) mView.getTag();

            return mView;

        }

像这样更改你的getview。希望这会对你有所帮助。