java - Android ListView TextView

标签 java android listview

我一直在努力让 ListView 正确显示。

为了澄清,我需要在 ListView 上方有一个 ScrollView ,这就是我没有扩展列表 fragment 的原因。

我需要标题和描述是单独的元素。我可以看到我是如何将他们放入类的,所以我想我已经成功了一半。如果有人能帮助我那就太好了。

PhotosFragment.java 包info.androidhive.slidingmenu;

import android.app.Fragment;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class PhotosFragment extends Fragment {
    ListView list;
    TextView pm;
    TextView sp;
    ArrayList<HashMap<String, String>> mlist = new ArrayList<HashMap<String,String>>();
    private static final String TAG_APPLE = "Apple";
    private static final String TAG_PHONEMODEL = "PhoneModel";
    private static final String TAG_SPECS = "specs";
    JSONArray model = null;

    public PhotosFragment(){}

    ListView mListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_photos, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setHasOptionsMenu(true);

        mListView = (ListView) getView().findViewById(R.id.mListView);

        new RequestTask().execute("http://www.horwichadvertiser.co.uk/app/index.php?Type=8&catid=7&userid=4");

    }

    private static final String TAG = MainActivity.class.getSimpleName();

    JSONObject obj;
    JSONArray stories;

    public class RequestTask extends AsyncTask<String, String, String> {
        private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pm = (TextView)getView().findViewById(R.id.phonemodel);
            sp = (TextView)getView().findViewById(R.id.specification);

            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... uri) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response;
            String responseString = null;
            try {

                response = httpclient.execute(new HttpGet(uri[0]));
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    response.getEntity().writeTo(out);

                    out.close();
                    responseString = out.toString();

                    //Log.d(TAG, responseString);

                } else {
                    //Closes the connection.
                    response.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            } catch (ClientProtocolException e) {
                //TODO Handle problems..
               // Log.d(TAG, String.valueOf(e));
            } catch (IOException e) {
                //TODO Handle problems..
              //  Log.d(TAG, String.valueOf(e));
            }
            return responseString;
        }

        @Override
        protected void onPostExecute(String result) {
            pDialog.dismiss();
            super.onPostExecute(result);

            //Log.e(TAG, result);
            try {
                obj = new JSONObject(result);
                stories = obj.getJSONArray("stories");

                // initialize the items list
                List<ListViewItem> mItems = new ArrayList<ListViewItem>();

                for (int i = 0; i < stories.length(); i++) {

                    JSONObject storyObj = stories.getJSONObject(i);

                    if (!storyObj.has("Advert")) {

                        newsStories newsStories = new newsStories();

                        newsStories.setTitle(storyObj.getString("subject"));
                        newsStories.setBody(storyObj.getString("body"));

                        mItems.add(new ListViewItem(newsStories.getTitle(), newsStories.getBody(), ""));

                    }
                    else {
                        newsStories newsStories = new newsStories();
                        newsStories.setThumbnailUrl(storyObj.getString("Advert"));
                        mItems.add(new ListViewItem("", "", newsStories.getThumbnailUrl()));

                    }
                }

                ArrayAdapter<ListViewItem> adapter = new ArrayAdapter<ListViewItem>(getActivity(), android.R.layout.simple_list_item_1, mItems);

                mListView.setTextFilterEnabled(true);
                // ArrayAdapter<ListViewItem> adapter = new ArrayAdapter<ListViewItem>(getActivity(), android.R.layout.two_line_list_item, mItems);
                //mListView.setAdapter(adapter);
                mListView.setAdapter(adapter);

            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }


        public class newsStories {
            private String name, thumbnailUrl, body;

            public String getTitle() {
                return name;
            }

            public void setTitle(String name) {
                this.name = name;
            }

            public String getBody() {
                return body;
            }

            public void setBody(String body) {

                this.body = body.substring(0, 150);

            }


            public String getThumbnailUrl() {
                return thumbnailUrl;
            }

            public void setThumbnailUrl(String thumbnailUrl) {
                //Check if thumbnail exists, if so take out spaces and replace with -

                this.thumbnailUrl = thumbnailUrl;

            }

        }

    }

    public class ListViewItem{
        public final String title;        // the text for the ListView item title
        public final String description;  // the text for the ListView item description
        public final String adUrl;
        //public final Drawable image;

        public ListViewItem(String title, String description, String Advert_Url) {



            if (Advert_Url != null && !Advert_Url.isEmpty()) {

                String Advert = "http://www.horwichadvertiser.co.uk/images/horwichadvertiserlogo.png?width=700&height=200";





                this.title = "";
                this.description = "";
                this.adUrl = Advert;

            } else {
                this.title = title;
                this.description = description;
                this.adUrl = "";
            }


        }



        @Override
        public String toString() {
            return this.title + ".\r\n" + Html.fromHtml(this.description);
        }
    }


}

Fragment_Photos.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="150dp" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button2" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button3" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button4" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button5" />

        </LinearLayout>
    </HorizontalScrollView>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="50dp"
        android:id="@+id/relativeLayout">

        <TextView
            android:id="@+id/body"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="22px" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:textSize="13px" />
    </RelativeLayout>
    <ListView
        android:id="@id/mListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@+id/relativeLayout">
    </ListView>
</RelativeLayout>

最佳答案

您的资源 xml 不正确。

1.

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

android:orientationLinearLayout 一起使用,而不是 RelativeLayout

2.

    <ListView
        android:id="@id/mListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@+id/relativeLayout">
    </ListView>

不正确使用 android:layout_below - 值应为现有 id:“@id/relativeLayout”(@ 后不带 +)。

android:id="@id/mListView" 分配的 ID 不正确。如果是新ID,则应为 android:id="@+id/mListView"

您可能想使用LinearLayout而不是relative

  • fill_parent 已过时,直到您针对某些 API 7 进行开发。应该改为 match_parent
  • 关于java - Android ListView TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28049365/

    相关文章:

    android - 在谷歌地图上显示 ListView 项目点击位置

    c# - 添加数据后如何滚动到 ListView 的末尾 - WPF

    java - 如何在java中获得整数的0填充二进制表示?

    java - 如何删除持久/非持久集合字段为空的对象?

    java - 如何捕获 WTObject 的修订事件?

    java - 为什么我不能设置不同包中类似类的对象列表

    android - 如何在Android中使用jess

    java - 终止后台线程内长时间运行的任务

    java - C2DM发送消息缓慢

    wpf - 如何访问 WPF ListView 的 ListViewItems?