我已经解析了一个 xml 并在列表视图中使用它们。但有一个问题。我会在这里写两节课。其中之一是我的自定义适配器类HaberAdapter.java 。
package haberler;
import java.util.Currency;
import com.applogist.kusadasibelediyesi.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HaberAdapter extends BaseAdapter implements OnItemClickListener {
HaberRSS haberrss = null;
private ImageLoader imageloader;
Context context;
private LayoutInflater inflater = null;
public HaberAdapter(Context context,HaberRSS haberrss) {
this.context = context;
this.haberrss = haberrss;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageloader = ImageLoader.getInstance();
imageloader.init(ImageLoaderConfiguration.createDefault(context));
}
@Override
public int getCount() {
return haberrss.getItems().size();
}
@Override
public HaberlerItems getItem(int position) {
return haberrss.getItems().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.haber_rss_item, null);
HaberlerItems currentItem = (HaberlerItems)getItem(position);
TextView lblTitle = (TextView) vi.findViewById(R.id.lblTitle);
TextView lblDescription = (TextView) vi.findViewById(R.id.lblDescription);
ImageView imgHaber = (ImageView) vi.findViewById(R.id.imgHaber);
lblTitle.setText(currentItem.getTitle());
lblDescription.setText(currentItem.getDescription());
imageloader.displayImage(currentItem.getImageUrl(), imgHaber);
return vi;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, String.valueOf(position), Toast.LENGTH_SHORT).show();
}
}
物品是我从中得到的
public class HaberlerItems {
private String title;
private String description;
private String shortDesc;
private String dateAdded;
private List<HaberlerImage> imageList;
private String imageUrl;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public List<HaberlerImage> getImageList() {
return imageList;
}
public void setImageList(List<HaberlerImage> imageList) {
this.imageList = imageList;
}
public HaberlerItems() {
setTitle(null);
setDescription(null);
setShortDesc(null);
setDateAdded(null);
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setDescription(String description) {
this.description= description;
}
public String getDescription() {
return description;
}
public String getShortDesc() {
return shortDesc;
}
public void setShortDesc(String shortDesc) {
this.shortDesc = shortDesc;
}
public String getDateAdded() {
return dateAdded;
}
public void setDateAdded(String dateAdded) {
this.dateAdded = dateAdded;
}
}
当我执行应用程序时,listview 上没有显示任何内容。换句话说,显示的是一个空的列表视图。我找不到问题。
还有一个haber_rss_item.xml是;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/lnMain"
android:layout_width="match_parent"
android:layout_height="100dip"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/lnUst"
android:layout_width="match_parent"
android:layout_height="20dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:gravity="left|center" >
<TextView
android:id="@+id/lblTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:text=""
android:textColor="@color/blue"
android:textSize="14sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/lnAlt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:gravity="center"
android:weightSum="10" >
<LinearLayout
android:id="@+id/lnImage"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3" >
<ImageView
android:id="@+id/imgHaber"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY" >
</ImageView>
</LinearLayout>
<LinearLayout
android:id="@+id/lnDescription"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="6" >
<TextView
android:id="@+id/lblDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="2dip"
android:maxLines="5"
android:text=""
android:textColor="@color/black"
android:textSize="12sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/lnOk"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
</LinearLayout>
</LinearLayout>
</LinearLayout>