0

我已经解析了一个 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>

4

2 回答 2

0

你的R.layout.haber_rss_item包含R.id.lblTitle. 有时,当View引用来自不同布局的内容时,它不会显示错误。但是,在运行时抛出NullPointerException

建议:发帖StackTrace会有所帮助。

于 2013-06-26T10:41:01.707 回答
0
try something like this ..   

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.HashMap;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.NodeList;

    import android.app.ListActivity;
    import android.content.Intent;
    import android.graphics.drawable.GradientDrawable;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;

    import com.google.ads.AdRequest;
    import com.google.ads.AdView;

    public class AndroidXMLParsingActivity extends ListActivity {

        static final String KEY_ITEM = "item"; // parent node
        static final String KEY_ID = "id";
        static final String KEY_NAME = "name";
        static final String KEY_ADDRESS = "address";
        static final String KEY_PHONE = "phone";
        static final String KEY_ATTRACTION = "attraction";
        String xml;
        int mIndex;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.display);
            Intent in = getIntent();
            mIndex = Integer.parseInt(in.getStringExtra("value"));

            ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
            XMLParser parser = new XMLParser();
            switch (mIndex) {
            case 0:
                InputStream stream = getResources()
                        .openRawResource(R.raw.myxml);
                try {
                    byte[] buffer = new byte[stream.available()];
                    stream.read(buffer);
                    stream.close();
                    xml = new String(buffer);
                } catch (IOException e) {
                    // Error handling
                }
                break;

            }

            Document doc = parser.getDomElement(xml); // getting DOM element
            NodeList nl = doc.getElementsByTagName(KEY_ITEM);
            for (int i = 0; i < nl.getLength(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                map.put(KEY_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
                map.put(KEY_PHONE, parser.getValue(e, KEY_PHONE));
                map.put(KEY_ATTRACTION, parser.getValue(e, KEY_ATTRACTION));
                menuItems.add(map);
            }

            ListAdapter adapter = new SimpleAdapter(this, menuItems,
                    R.layout.list_item, new String[] { KEY_NAME, KEY_ADDRESS,
                            KEY_PHONE, KEY_ATTRACTION }, new int[] { R.id.name,
                            R.id.desciption, R.id.cost, R.id.sttraction });

            setListAdapter(adapter);
            ListView lv = getListView();

            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String val = "" + mIndex;
                    Intent i = new Intent(getApplicationContext(),
                            MallFinderActivity.class);
                    i.putExtra("value", val);
                    i.putExtra("position", "" + position);
                    String mallName = ((TextView) view.findViewById(R.id.name))
                            .getText().toString();
                    i.putExtra("mall", mallName);
                    startActivity(i);
                }
            });
        }
    }

this is my listitem 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="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <!-- Name Label -->

            <TextView
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="2dip"
                android:paddingTop="6dip"
                android:textColor="#dc6800"
                android:textSize="16sp"
                android:textStyle="bold" />
            <!-- Description label -->

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:text="Address: "
                    android:textColor="#5d5d5d"
                    android:textStyle="bold" >
                </TextView>

                <TextView
                    android:id="@+id/desciption"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingBottom="2dip"
                    android:textColor="#000000" >
                </TextView>
            </LinearLayout>
            <!-- Linear layout for cost and price Cost: Rs.100 -->

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <!-- Cost Label -->

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:text="Phone: "
                    android:textColor="#5d5d5d"
                    android:textStyle="bold" >
                </TextView>
                <!-- Price Label -->

                <TextView
                    android:id="@+id/cost"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:textColor="#000000"
                    android:textStyle="bold" >
                </TextView>
            </LinearLayout>

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <!-- Cost Label -->

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:text="Attraction: "
                    android:textColor="#5d5d5d"
                    android:textStyle="bold" >
                </TextView>
                <!-- Price Label -->

                <TextView
                    android:id="@+id/sttraction"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:textColor="#000000"
                    android:textStyle="bold" >
                </TextView>
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>


   this is main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/mainlayoutsecond"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight=".1"
        android:orientation="vertical" >

        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:cacheColorHint="#E3E3E3"
            android:divider="#b5b5b5"
            android:dividerHeight="1dp"
            android:fastScrollEnabled="true"
            android:focusable="false" />
    </LinearLayout>
    </LinearLayout>
于 2013-06-26T11:03:20.610 回答