gpt4 book ai didi

android - 如何使用 ListAdapter 在 ListView 中打印数据

转载 作者:行者123 更新时间:2023-11-29 22:29:23 25 4
gpt4 key购买 nike

已更新

我正在尝试在 ListView 中打印从数据库中检索到的数据。有一段时间我的应用程序打印数据库中的每个数据, ListView 上的一行。所以如果数据库中有 10 条数据,应用程序打印 10 行,对应于数据库的每一行。这是打印方式的 View 。

现在的新形象:

alt text

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

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

好吧,我将此 Activity 命名为 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 的 View 的 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>

所以我想在 Activity 的 TextView 中打印数据,但我不知道在哪里设置文本,在什么类中,是在 MyListAdapter 类中还是在 Activity 中。

谢谢。

最佳答案


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

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

谢谢…………

关于android - 如何使用 ListAdapter 在 ListView 中打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4774596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com