1

更新

我正在尝试在列表视图上打印从数据库中检索到的数据。有一段时间,我的应用程序为数据库中的每个数据打印列表视图上的一行。所以如果数据库中有 10 条数据,app 打印 10 行,对应数据库的每一行。这是如何打印的视图。

它现在看起来的新图像:

替代文字

但是,它没有按我的意愿打印数据。我想在特定的文本视图中打印该行的一列,但它没有显示任何内容。

因此,活动 RatedCalss 调用该方法selectTopCalls()并使 List 接收该方法返回的列表。然后我将此列表传递给 listAdapter。

好吧,我有这个名为RatedCalls.java的活动:

public class RatedCalls extends Activity {

private static final String LOG_TAG = "RatedCalls";
private CallDataHelper cdh;
private ListView listview;
private ArrayList<String> ratedCallsList;
private MyListAdapter listAdapter;
private View view;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    Log.i(LOG_TAG, "calling from onCreate()");
    cdh = new CallDataHelper(this);

    startService(new Intent(this, RatedCallsService.class));

    setBasicContent();

}

public void setBasicContent() {
    listview = (ListView) findViewById(R.id.list_view);     

    ratedCallsList = this.cdh.selectTopCalls();     

    Log.i(LOG_TAG, "ratedCallsList size: " + ratedCallsList.size());        
    listAdapter = new MyListAdapter(this, this, R.id.list_view, ratedCallsList);
    listview.setAdapter(listAdapter);   
   }
}

我有这个类,一个名为MyListAdapter.java的 ListAdapter 类:

    public class MyListAdapter extends ArrayAdapter { //--CloneChangeRequired

    private ArrayList mList;
    private Context mContext;
    private Activity mActivity; 
    private int selectedPos = -1;   // init value for not-selected
    private ArrayList<String> ratedCallsList;
    private CallDataHelper cdh;

    public void setSelectedPosition(int pos){
        selectedPos = pos;
        notifyDataSetChanged();
    }
    public int getSelectedPosition(){
        return selectedPos;
    }

    public MyListAdapter(Context context, Activity activity, int textViewResourceId, ArrayList list) {
        super(context, textViewResourceId, list);
        this.mList = list;
        this.mContext = context;
        this.mActivity = activity;
    }

    public View getView(int position, View convertView, ViewGroup parent){
        View view = convertView;
        try{

            if (view == null) {
                LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = vi.inflate(R.layout.list_item, null);    // --CloneChangeRequired(list_item)
            }

            // setting STRIP BG         
            if(position == selectedPos){
                view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(062, 076, 120) );
            }else if(position%2==0){
                view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(226, 231, 239) );
            }else{
                view.findViewById(R.id.rlt_main).setBackgroundColor( Color.rgb(200, 210, 223) );
            }   

            setViews(position, view);

        }catch(Exception e){
            //Log.i(MyListAdapter.class.toString(), e.getMessage());                
        }
        return view;
    }   

    public void setViews(int position, View view) {         

        cdh = new CallDataHelper(mContext);

        if(mContext.getClass().equals((RatedCalls.class))){

            ratedCallsList = this.cdh.selectTopCalls();
            Log.i("MYLISTADAPTER", "size " + ratedCallsList.size());
            if (ratedCallsList != null) {

                    ((TextView) view.findViewById(R.id.contact_name)).setText(ratedCallsList.get(0));
                    ((TextView) view.findViewById(R.id.phone_number)).setText(ratedCallsList.get(1));
                    ((TextView) view.findViewById(R.id.duration)).setText(ratedCallsList.get(2));
                    ((TextView) view.findViewById(R.id.date)).setText(ratedCallsList.get(3));
            }

        }else if(mContext.getClass().equals(RatedContacts.class)){

            final PublishersBO listPublisher = (PublishersBO) mList.get(position);
            if (listPublisher != null) {
                //--setting list_item views
                ((TextView) view.findViewById(R.id.contact_name)).setText(listPublisher.getName());
                ((TextView) view.findViewById(R.id.phone_number)).setText(listPublisher.getEmail());
                //--onClickListener
                view.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) {
                        Intent myIntent = new Intent(mContext,RatedContacts.class);
                        myIntent.putExtra("NAME", listPublisher.getName());                                                     
                        myIntent.putExtra("ACTIVITY_NAME", mContext.getClass().toString());
                        mContext.startActivity(myIntent);
                        mActivity.finish();
                    }
                });
            }

        }       
    }
}

从数据库中检索数据的方法位于处理 SQLite 函数的单独类中。这是方法:

public ArrayList<String> selectTopCalls() {

    ArrayList<String> list1 = new ArrayList<String>();
    Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null,
            "duration desc");

    if (cursor.moveToFirst()) {
        do {                
            //if (cursor.getString(2) != "") {

                cdObj = new CallData();

                list1.add(cursor.getString(2));
                list1.add(cursor.getString(4));
                list1.add(cursor.getString(5));
                list1.add(cursor.getString(6));                     

        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list1;
}

这是名为list_item.xml的视图的 xml 文件:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="67px" 
android:id="@+id/rlt_main"
android:layout_toRightOf="@+id/iv_forward">

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true"
    android:id="@+id/iv_forward" 
    android:src="@drawable/icon"
    android:layout_alignParentLeft="true">
</ImageView>                

<TextView 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/phone_number" 
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/iv_forward"
    android:layout_alignBottom="@+id/iv_forward">
</TextView> 

<TextView 
    android:layout_height="wrap_content"        
    android:layout_width="wrap_content" 
    android:id="@+id/duration" 
    android:layout_alignBottom="@+id/phone_number"
    android:layout_alignRight="@+id/phone_number"
    >
</TextView>

<TextView 
    android:layout_height="wrap_content"        
    android:layout_width="wrap_content" 
    android:id="@+id/date" 
    android:layout_alignBottom="@+id/contact_name"
    android:layout_alignRight="@+id/contact_name"
    >
</TextView>

<TextView 
    android:layout_height="wrap_content"  
    android:layout_width="fill_parent" 
    android:id="@+id/contact_name" 
    android:textSize="20px" 
    android:layout_toRightOf="@+id/iv_forward"
    android:layout_alignParentTop="true">
</TextView>

所以我想在活动的文本视图中打印数据,但我不知道在哪里设置文本,在什么类中,如果在 MyListAdapter 类中还是在活动中。

谢谢。

4

1 回答 1

2


我对您的代码做了一些更改,请查看这里,

https://gist.github.com/683b84af9d01bf18fe3d

谢谢............

于 2011-01-23T17:15:02.530 回答