无法在 ListView 中看到文本

发布于 2024-12-15 23:05:29 字数 2143 浏览 3 评论 0原文

我使用 ListActivity 来显示简单文本,并使用 SimpleCursorAdapter 从数据库中获取文本。我尝试调试该问题,发现光标成功获取结果,但文本未显示在 ListView 中。

以下是我正在使用的 ListActivity 代码。

public class MyListActivity extends ListActivity{

    Cursor myCursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        myCursor = getData();
        startManagingCursor(myCursor);

        ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.display_details, myCursor, new String[]{MyDbHelper.ID_FIELD, MyDbHelper.NAME_FIELD}, new int[]{R.id.row_id, R.id.row_name});

        setListAdapter(myAdapter);
    }

    private Cursor getData() {
        return MyDBHelper.getRoutes();
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        routesCursor.close();
    }
}

以下是 ListView 中每一行的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:id="@+id/row_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/row_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/row_id"
        android:layout_toRightOf="@+id/row_id"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>

,ListActivity 的布局如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent">

    </ListView>

</RelativeLayout>

请为我提供解决方案。提前致谢。

I am using ListActivity to display simple text with SimpleCursorAdapter to get the texts from my database. I have tried to debug the problem and found that the cursor is successfully fetching the result but the text is not displayed in the ListView.

Following is the ListActivity code I am using.

public class MyListActivity extends ListActivity{

    Cursor myCursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        myCursor = getData();
        startManagingCursor(myCursor);

        ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.display_details, myCursor, new String[]{MyDbHelper.ID_FIELD, MyDbHelper.NAME_FIELD}, new int[]{R.id.row_id, R.id.row_name});

        setListAdapter(myAdapter);
    }

    private Cursor getData() {
        return MyDBHelper.getRoutes();
    }


    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        routesCursor.close();
    }
}

and following is my xml file for each row in ListView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:id="@+id/row_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/row_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/row_id"
        android:layout_toRightOf="@+id/row_id"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</RelativeLayout>

and my layout for ListActivity is as following

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent">

    </ListView>

</RelativeLayout>

Please provide me the solution. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-12-22 23:05:29

这段代码可以帮助您解决问题。

package com.example.getkontakdata;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma;
Button select;
CheckBox cb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getAllCallLogs(this.getContentResolver());
    final ListView lv= (ListView) findViewById(R.id.lv);
    cb = (CheckBox) findViewById(R.id.checkBox1);

    ma = new MyAdapter();
    lv.setAdapter((ListAdapter) ma);
    lv.setOnItemClickListener(this); 
    lv.setItemsCanFocus(false);
    lv.setTextFilterEnabled(true);

    // adding
    select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            StringBuilder checkedcontacts= new StringBuilder();
            System.out.println(".............." + ma.mCheckStates.size());

            for(int i = 0; i < phno1.size(); i++) {
                if(ma.mCheckStates.get(i) == true) {
                    checkedcontacts.append(phno1.get(i).toString());
                    checkedcontacts.append(";");  

                } else {
                    System.out.println(".. Not Checked ......" + phno1.get(i).toString());
                }
            }
        }       
    });
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    ma.toggle(arg2);
}

public  void getAllCallLogs(ContentResolver cr) {
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext()) {
          String name=phones.getString
                  (phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString
                  (phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          //System.out.println(".................." + phoneNumber); 
          name1.add(name);
          phno1.add(phoneNumber);
        }
        phones.close();
 }

class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
    private SparseBooleanArray mCheckStates;
    //private SparseBooleanArray nCheckStates;
    LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    MyAdapter() {
        mCheckStates = new SparseBooleanArray(name1.size());
        mCheckStates = new SparseBooleanArray(phno1.size());
        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public String get(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
     View vi=convertView;
         if(convertView==null)
         vi = mInflater.inflate(R.layout.row, null); 

         TextView tv= (TextView) vi.findViewById(R.id.textView1);
         tv1= (TextView) vi.findViewById(R.id.textView2);
         cb = (CheckBox) vi.findViewById(R.id.checkBox1);

         tv.setText("Nama : "+ name1.get(position));
         tv1.setText("Nomor Kontak : "+ phno1.get(position));

         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
         cb.setOnCheckedChangeListener((OnCheckedChangeListener) this);

        return vi;
    }
     public boolean isChecked(int position) {
         return mCheckStates.get(position, false);
     }

     public void setChecked(int position, boolean isChecked) {
         mCheckStates.put(position, isChecked);
         System.out.println("hello...........");
         notifyDataSetChanged();
     }

     public void toggle(int position) {
         setChecked(position, !isChecked(position));
     }

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         // TODO Auto-generated method stub
         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    
     }
}
 }

This code can help your problem.

package com.example.getkontakdata;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
MyAdapter ma;
Button select;
CheckBox cb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getAllCallLogs(this.getContentResolver());
    final ListView lv= (ListView) findViewById(R.id.lv);
    cb = (CheckBox) findViewById(R.id.checkBox1);

    ma = new MyAdapter();
    lv.setAdapter((ListAdapter) ma);
    lv.setOnItemClickListener(this); 
    lv.setItemsCanFocus(false);
    lv.setTextFilterEnabled(true);

    // adding
    select = (Button) findViewById(R.id.button1);
    select.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            StringBuilder checkedcontacts= new StringBuilder();
            System.out.println(".............." + ma.mCheckStates.size());

            for(int i = 0; i < phno1.size(); i++) {
                if(ma.mCheckStates.get(i) == true) {
                    checkedcontacts.append(phno1.get(i).toString());
                    checkedcontacts.append(";");  

                } else {
                    System.out.println(".. Not Checked ......" + phno1.get(i).toString());
                }
            }
        }       
    });
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    ma.toggle(arg2);
}

public  void getAllCallLogs(ContentResolver cr) {
    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    while (phones.moveToNext()) {
          String name=phones.getString
                  (phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
          String phoneNumber = phones.getString
                  (phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
          //System.out.println(".................." + phoneNumber); 
          name1.add(name);
          phno1.add(phoneNumber);
        }
        phones.close();
 }

class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
    private SparseBooleanArray mCheckStates;
    //private SparseBooleanArray nCheckStates;
    LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    MyAdapter() {
        mCheckStates = new SparseBooleanArray(name1.size());
        mCheckStates = new SparseBooleanArray(phno1.size());
        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public String get(int position) {
        // TODO Auto-generated method stub
        return null;
    }

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

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
     View vi=convertView;
         if(convertView==null)
         vi = mInflater.inflate(R.layout.row, null); 

         TextView tv= (TextView) vi.findViewById(R.id.textView1);
         tv1= (TextView) vi.findViewById(R.id.textView2);
         cb = (CheckBox) vi.findViewById(R.id.checkBox1);

         tv.setText("Nama : "+ name1.get(position));
         tv1.setText("Nomor Kontak : "+ phno1.get(position));

         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
         cb.setOnCheckedChangeListener((OnCheckedChangeListener) this);

        return vi;
    }
     public boolean isChecked(int position) {
         return mCheckStates.get(position, false);
     }

     public void setChecked(int position, boolean isChecked) {
         mCheckStates.put(position, isChecked);
         System.out.println("hello...........");
         notifyDataSetChanged();
     }

     public void toggle(int position) {
         setChecked(position, !isChecked(position));
     }

     @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         // TODO Auto-generated method stub
         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    
     }
}
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文