我制作了一个自定义适配器,其中包含一个textview和一个webview作为listview中的行项。我从服务器获取数据。但我不确定它为什么只显示一行,尽管应该有两行。
活动类
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class GoldenTipsActivity extends ActionBarActivity {
ListView listView;
List<PatientUtility> patientUtilityArrayList;
CustomPatientUtilityAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_golden_tips);
listView = (ListView) findViewById(R.id.listView);
getData();
}
public void getData(){
GetData getData = new GetData();
getData.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_golden_tips, menu);
return true;
}
public List<PatientUtility> parseData(String jsonStringData) throws JSONException{
JSONObject object = new JSONObject(jsonStringData);
JSONArray array = object.getJSONArray("pat_data");
String title;
String data;
PatientUtility patientUtility = null;
for (int i = 0; i < array.length(); i++){
patientUtilityArrayList = new ArrayList<>();
JSONObject object1 = array.getJSONObject(i);
title = object1.getString("title");
data = object1.getString("data");
patientUtility = new PatientUtility();
patientUtility.setTitle(title);
patientUtility.setData(data);
Log.v("Title", patientUtility.getTitle());
Log.v("Data", patientUtility.getData());
patientUtilityArrayList.add(patientUtility);
}
return patientUtilityArrayList;
}
private class GetData extends AsyncTask<Void, Void, List<PatientUtility>>{
final String LOG_TAG = GetData.class.getSimpleName();
public String html2text(String html) {
return Jsoup.parse(html).text();
}
@Override
protected List<PatientUtility> doInBackground(Void... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String dataJsonString = null;
try {
final String BASE_URL = "http://mycityortho.com/display_golden_tips.php";
URL url = new URL(BASE_URL);
Log.v(LOG_TAG, url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
dataJsonString = buffer.toString();
Log.v(LOG_TAG, dataJsonString);
} catch (IOException e) {
Log.e(LOG_TAG, "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e(LOG_TAG, "Error closing stream", e);
}
}
}
try {
String textData = html2text(dataJsonString);
return parseData(textData);
}catch (JSONException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<PatientUtility> result) {
if (result != null){
adapter = new CustomPatientUtilityAdapter(GoldenTipsActivity.this ,result);
listView.setAdapter(adapter);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的自定义适配器类
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomPatientUtilityAdapter extends BaseAdapter{
private List<PatientUtility> list;
private Activity activity;
private LayoutInflater inflater;
public CustomPatientUtilityAdapter(Activity activity, List<PatientUtility> list){
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null){
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null){
convertView = inflater.inflate(R.layout.list_row_patient_utility_section, null);
}
TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
WebView dataTextView = (WebView) convertView.findViewById(R.id.dataTextView);
PatientUtility p = list.get(position);
titleTextView.setText(p.getTitle());
dataTextView.loadData(p.getData(), "text/html", "utf-8");
return convertView;
}
}
我确信会出现小问题,但我无法解决此问题并花费数小时来解决此错误。
答案 0 :(得分:0)
替换此
for (int i = 0; i < array.length(); i++){
patientUtilityArrayList = new ArrayList<>();
.................
.................
}
return patientUtilityArrayList;
到这个
patientUtilityArrayList = new ArrayList<>();
for (int i = 0; i < array.length(); i++){
.................
.................
}
return patientUtilityArrayList;
实际上你现在正在每个循环中创建新的ArrayList对象。因此,不是将数据添加到上一个列表,而是每次创建一个新列表并添加该列表。
答案 1 :(得分:0)
嗨,我认为你在public List<PatientUtility> parseData(String jsonStringData) {..} function
犯了错误。看初始化是错误的。
patientUtilityArrayList = new ArrayList<>();
在for循环中初始化的数组列表。这就是为什么你在列表视图中只有一个项目。请参阅更新的parseData函数。
public List<PatientUtility> parseData(String jsonStringData) throws JSONException{
JSONObject object = new JSONObject(jsonStringData);
JSONArray array = object.getJSONArray("pat_data");
String title;
String data;
PatientUtility patientUtility = null;
patientUtilityArrayList = new ArrayList<>();
for (int i = 0; i < array.length(); i++){
JSONObject object1 = array.getJSONObject(i);
title = object1.getString("title");
data = object1.getString("data");
patientUtility = new PatientUtility();
patientUtility.setTitle(title);
patientUtility.setData(data);
Log.v("Title", patientUtility.getTitle());
Log.v("Data", patientUtility.getData());
patientUtilityArrayList.add(patientUtility);
}
return patientUtilityArrayList;
}
答案 2 :(得分:0)
这是关于您的编码风格的答案。 Google
建议我们覆盖getView
的{{1}},如下所示:
BaseAdapter