-1

现在我尝试使用适配器。但我不明白如何从数据中设置值。因为在朋友 = db.selectall 中,朋友中的值有 3 个值(fname,lname,nickname)。所以我的问题是如何设置值(fname/lname/nickname 或一个或另一个)我的代码现在看起来像这样 ::::

package com.example.sqlite;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sqlite.db.FriendsDB;
import com.example.sqlite.entry.FriendEntry;

public class FriendsListActivity extends Activity {
private Context context;
private FriendsDB db;
private ArrayList<FriendEntry> friends;
    private ArrayList<String> data;
private TextView hellotext;
private ListView hellolistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.friendlist_layout);

}
public void showAllList(){
    //view matching
    hellotext = (TextView) findViewById(R.id.hellotext);
    hellolistview = (ListView) findViewById(R.id.hellolistview);
    //select data
    friends = db.selectAll();
        if(friends.size()==0){
            Toast.makeText(context,"You dont have any friend.",Toast.LENGTH_SHORT).show();
        }else{
            data = new ArrayList<String>();
            for (int i = 1;i<=friends.size();i++){
            // set value for data   


            **data.add("Your Name is "+friends["fname"]);<< I want to add data like this .How to correct**  


            }
        }
    }

private class adapter extends BaseAdapter{
    private Holder holder;
    @Override
        //ดาต้ามีกี่แถว
    public int getCount() {
        // TODO Auto-generated method stub
        return friends.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        //create
        if( view == null){
            view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_layout,null);
            holder = new Holder();
            holder.title = (TextView) view.findViewById(R.id.item_title);
            view.setTag(holder);
        }else{
            holder = (Holder) view.getTag();
        }

        //assign data / wait for data 

        return null;
    }

    private class Holder{
            //view แต่ละตัวเก็บค่าอะไรบ้าง
        public TextView title;
    }
}


}
4

2 回答 2

0

你做得很好,但我建议你使用 HashMap 类型的 ArrayList 而不是使用 Friends 类。

这将减轻您的申请负担。

ArrayList<HashMap<Object,String>> list=new  ArrayList<HashMap<Object,String>>();
HashMap<Object,String> hm;

在您的全选方法中

do{
   hm=new HashMap<Object,String>();
   hm.add(Key_Name,"retrieve the value from cursor here");
   list.add(hm);
 }while(c.movetonect());
return list;

在你的活动中

    ArrayList<HashMap<Object,String>> list=new  ArrayList<HashMap<Object,String>>();
    list=db.selectAll();
    HashMap<Object,String> hm;
    for (int i=0;i<list.length;i++){
     hm=list.getIndex(i);    //retrieve all the vaalues  here

    }

使用接受列表的列表适配器来填充列表视图

于 2013-08-20T21:14:22.330 回答
0

当您有数据Cursor并希望将其显示在 a 中ListView时,您需要使用 a CursorAdapter

您可以使用预定义的SimpleCursorAdapter视图,或者如果您想要自定义视图,您可以扩展CursorAdapter该类。

教程在这里:http ://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/

于 2013-08-20T18:35:59.020 回答