android listactivity和baseadapter
我有一个 Listativty,我需要在每一行中显示图像和文本。
布局如下所示:
event.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">
<ImageView android:layout_height="wrap_content"
android:layout_width="30px"
android:layout_marginTop="2px" android:layout_marginRight="2px"
android:layout_marginLeft="2px" android:id="@+id/logoImage"
>
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/title"
android:textSize="15px"></TextView>
</LinearLayout>
活动如下所示:
public class Activity extends ListActivity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
dba=new DatabaseAdapter(this);
dba.open();
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView lv = getListView();
lv.addHeaderView(header);
fillOnStart();
}
private void fillOnStart(){
ArrayList<Title> temp = dba.returnTitle(); // this works
ArrAdapter notes = new ArrAdapter(this, temp);
Toast.makeText(this, "Size: " +notes.getCount(), Toast.LENGTH_LONG).show();
//because this show good size
setListAdapter(notes); //i suppose this is a problem
}
我的适配器如下所示:
private class ArrAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Title> lista;
public ArrAdapter(Context c, ArrayList<Title> list){
mContext = c;
this.lista =list;
}
public int getCount() {
// TODO Auto-generated method stub
return lista.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder vh =null;
//Title title= al.get(position);
LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.event, parent, false);
ImageView imlogo = (ImageView)v.findViewById(R.id.logoImage);
TextView textTitle = (TextView)v.findViewById(R.id.tytul);
textTitle.setText("asdasdasdasd"); //i try like that, to make sure that my databaseAdapter do not have blame on it
convertView.setTag(v);
return convertView;
}
}
当我启动应用程序时,我看到“...意外停止...” - 我错过了什么?
更新:
我不使用 logcat。当我运行 logcat 并运行程序时,表中没有任何内容。也许我对 logcat 做错了什么?
当我尝试启动 logcat 时,我收到以下消息:
[2011-11-23 10:26:30 - Logcat]device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:736)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373)
at com.android.ddmlib.Device.executeShellCommand(Device.java:284)
at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:527)
好的,我的列表视图显示文本。这有效 View v = inflater.inflate(R.layout.event, null);,
但我在 logoImage 中看不到图像。
类标题看起来像:
public class Title {
public int id;
public String tit;
public Bitmap logo;
... and setter and getter
}
方法返回 Arraylist (在 dba 中),标题看起来像这样:
public ArrayList<Title> returnTitle(){
Title t = new Title(1,"aaaaaaaaaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\favicon.ico"));
Title t1 = new Title(2,"assdsdsdsdaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\icon.ico"));
ArrayList<Title> art = new ArrayList<Title>(2);
art.add(t);
art.add(t1);
return art;
}
另一方面,我可以动态地将图像插入到 fodler 例如drawable-hdpi,然后插入到我的 imageLogo 中吗?
I have a Listacitivty, where I need to show a image and text in every row.
The layout looks like this:
event.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">
<ImageView android:layout_height="wrap_content"
android:layout_width="30px"
android:layout_marginTop="2px" android:layout_marginRight="2px"
android:layout_marginLeft="2px" android:id="@+id/logoImage"
>
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/title"
android:textSize="15px"></TextView>
</LinearLayout>
The activity looks like this:
public class Activity extends ListActivity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
dba=new DatabaseAdapter(this);
dba.open();
View header = getLayoutInflater().inflate(R.layout.header, null);
ListView lv = getListView();
lv.addHeaderView(header);
fillOnStart();
}
private void fillOnStart(){
ArrayList<Title> temp = dba.returnTitle(); // this works
ArrAdapter notes = new ArrAdapter(this, temp);
Toast.makeText(this, "Size: " +notes.getCount(), Toast.LENGTH_LONG).show();
//because this show good size
setListAdapter(notes); //i suppose this is a problem
}
and my adapter looks like this:
private class ArrAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Title> lista;
public ArrAdapter(Context c, ArrayList<Title> list){
mContext = c;
this.lista =list;
}
public int getCount() {
// TODO Auto-generated method stub
return lista.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder vh =null;
//Title title= al.get(position);
LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.event, parent, false);
ImageView imlogo = (ImageView)v.findViewById(R.id.logoImage);
TextView textTitle = (TextView)v.findViewById(R.id.tytul);
textTitle.setText("asdasdasdasd"); //i try like that, to make sure that my databaseAdapter do not have blame on it
convertView.setTag(v);
return convertView;
}
}
When i start app I see "...stopped unexpectedly..." - what am I missing?
Update:
i don't use a logcat. WHen i run logcat and run program there is nothing in the table. maybe I do something wrong with logcat?
when i try to start logcat i get this messages:
[2011-11-23 10:26:30 - Logcat]device not found
com.android.ddmlib.AdbCommandRejectedException: device not found
at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:736)
at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373)
at com.android.ddmlib.Device.executeShellCommand(Device.java:284)
at com.android.ddmuilib.logcat.LogPanel$3.run(LogPanel.java:527)
ok my listview show a text. this works View v = inflater.inflate(R.layout.event, null);,
but i don't see images in logoImage.
class title looks like:
public class Title {
public int id;
public String tit;
public Bitmap logo;
... and setter and getter
}
and method returns Arraylist (in dba) with Title look like that:
public ArrayList<Title> returnTitle(){
Title t = new Title(1,"aaaaaaaaaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\favicon.ico"));
Title t1 = new Title(2,"assdsdsdsdaa", BitmapFactory.decodeFile("C:\\Users\\hormon\\Desktop\\icon.ico"));
ArrayList<Title> art = new ArrayList<Title>(2);
art.add(t);
art.add(t1);
return art;
}
in otherhand, can I dynamically insert a images to fodler e.g. drawable-hdpi, and then insert to my imageLogo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能在 getView 中您需要返回“v”本身而不是使用convertview.setTag(v)。我认为如果您观察 logcat 输出,您可能会收到 NullPointerException,因为 Convertview 本身将为 null。
probably in getView you need to return "v" itself rather than using convertview.setTag(v). I think if you watch you logcat output, you might be getting NullPointerException as convertview itself will be null.