更新
我正在尝试在列表视图中打印从数据库中检索到的数据。有一段时间我的应用程序打印数据库中的每个数据,列表视图上的一行。因此,如果数据库中有10个数据,则应用程序将打印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类中或在活动中。
感谢。