我正在尝试从sqlite获取数据到listview,所以我按照这个url的答案中的示例 Make listview from SQLite database
CustomCursorAdapter就像那样
public class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.topics, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.listLabel);
if (name_text != null) {
name_text.setText(name);
}
}
}
但onCreate我有这个代码
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
使用xml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="@+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="15sp" >
</TextView>
</LinearLayout>
问题是.. listView是空的!! ..我不知道为什么!!
答案 0 :(得分:2)
您正在参考布局主题。您应该在xml中使用id定义listview。 在setCOntentView(R.layout.topics)之后的onCreate()活动中,fins listview的id并将适配器设置为你的列表。
在你的xml中说topic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#0095FF">
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="@+id/lv">
</ListView>
// other ui elements
</LinearLayout>
然后在你的活动onCreate()
setContentview(R.layout.topics);
ListView listView = (ListView)findViewById(R.id.lv);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
来自聊天中的讨论
编辑:
定义main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#0095FF">
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="@android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp"
android:divider="#000000"
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>
在你的活动中
public class MainActivity extends Activity {
ListView lv;
CustomCursorAdapter cus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.list);
cus= new CustomCursorAdapter(parameters);
lv.setAdapter(cus);
}
}
定义customCursor适配器为自定义布局充气。设置图像视图的图像和textview的文本。
答案 1 :(得分:0)
尝试编辑xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="@+id/main"
>
<ImageView
android:id="@+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="@+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="15sp" >
</TextView>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/topics"
></ListView>
</LinearLayout>
在Activity onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
}