gpt4 book ai didi

java - Android ListView TextView

转载 作者:太空宇宙 更新时间:2023-11-04 14:13:06 24 4
gpt4 key购买 nike

我一直在努力让 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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com