简体   繁体   English

带有ArrayAdapter的Android ListView不显示任何内容

[英]Android ListView with ArrayAdapter doesn't show anything

I know there are a lot of posts on how to make an Android ListView, but even after looking through most of them I can't figure out what my problem is. 我知道有很多关于如何制作Android ListView的文章,但是即使仔细阅读了大多数文章,我仍然无法弄清我的问题是什么。 I'm completely new to Android and obviously a bit overwhelmed. 我对Android完全陌生,显然有点不知所措。 The activity runs but without showing any content. 该活动将运行,但不显示任何内容。

This is my Activity: 这是我的活动:

package com.example.myfirstapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class HistoryActivity extends Activity {

CustomRowAdapter customRowAdapter;  
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    listView = (ListView)findViewById(R.id.listView);
    customRowAdapter = new CustomRowAdapter(getApplicationContext());
    listView.setAdapter(customRowAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.history, menu);
    return true;
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

this is activity_history.xml: 这是activity_history.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"> 

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

The xml for the input in the ListView (custom_row.xml): ListView(custom_row.xml)中输入的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"> 

 <TextView
    android:id="@+id/textView1"
    android:background="@drawable/box"
    android:textSize="18sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name" />

 <TextView
    android:id="@+id/textView2"
    android:background="@drawable/box"
    android:textSize="18sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name" />

 <TextView
    android:id="@+id/textView3"
    android:background="@drawable/box"
    android:textSize="18sp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name" />

</LinearLayout>

and CustomRowAdapter.java: 和CustomRowAdapter.java:

package com.example.myfirstapp;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomRowAdapter extends ArrayAdapter<String> {

private String[] text1 = {
        "Google Plus",
        "Twitter", 
        "Facebook", 
        "Instagram"};

private String[] text2 = {
        "test1",
        "test2", 
        "test3", 
        "test4"};

private String[] text3 = {
        "Google Plus",
        "Twitter", 
        "Facebook", 
        "Instagram"};

private LayoutInflater layoutInflater;
private Context context;

public CustomRowAdapter(Context context) {
    super(context,0);
    layoutInflater=    (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.context=context;


}
@Override
public View getView(int position, View view, ViewGroup viewGroup){
    view = layoutInflater.inflate(R.layout.custom_row,null);
    TextView textViewT1 = (TextView)view.findViewById(R.id.textView1);
    TextView textViewT2 = (TextView)view.findViewById(R.id.textView2);
    TextView textViewT3 = (TextView)view.findViewById(R.id.textView3);

//      textViewT1.setText("test");
//      textViewT2.setText(text2[0]);
//      textViewT3.setText(text3[0]);


    return view; 
}

@Override
public int getCount(){
    return 0;
}       
}

Hope somebody can help me out. 希望有人可以帮助我。

Your ListView is not showing nothing because of 由于以下原因,您的ListView没有显示任何内容

@Override
public int getCount(){
    return 0;
}  

getCount has to return the number of elements that belong to your dataset. getCount必须返回属于您的数据集的元素数。 For instance 例如

 @Override
    public int getCount(){
        return text1.length;
    }  

You're returning 0 from getCount . 您将从getCount返回0。 Return the number of items in the list here, I suspect you want something like text1.length . 在此处返回列表中的项目数,我怀疑您想要类似text1.length东西。

Hey i see my simple and best example, Follow some steps 嘿,我看到了我最好的示例,请按照以下步骤操作

I mostly use custom for testing 我主要使用自定义进行测试

JSON Parsing Step 1. JSON解析步骤1。

JSONParser.java JSONParser.java

  public class JSONParser {
  InputStream is = null;
  JSONObject jObj = null;
  String json = "";

  // constructor
 public JSONParser() {
 }

  public String getJSONFromUrl(String url) {

 // Making HTTP request
 try {

   DefaultHttpClient httpClient = new DefaultHttpClient();
   HttpPost httpPost = new HttpPost(url);
   HttpResponse httpResponse = httpClient.execute(httpPost);
   Log.e("response", "" + httpResponse);

   HttpEntity httpEntity = httpResponse.getEntity();
   is = httpEntity.getContent();

   } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
   } catch (ClientProtocolException e) {
  e.printStackTrace();
} catch (IOException e) {
 e.printStackTrace();
}
try {
 BufferedReader reader = new BufferedReader(new InputStreamReader(
 is, "iso-8859-1"), 8);
 StringBuilder sb = new StringBuilder();
  String line = null;
 while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
 }
  json = sb.toString();
 is.close();
 } catch (Exception e) {
 Log.e("Buffer Error", "Error converting result " + e.toString());
 }
 return json;
 }


 }

Sept 2. 9月2日。

MainHome.java MainHome.java

   public class MainActivity extends Activity {

ListView lvdata;
LinkedList<HashMap<String, String>> aldata = new LinkedList<HashMap<String, String>>();

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();

 new getData().execute();
 }

 void init() {
 lvdata = (ListView) findViewById(R.id.lvdata);

 }

private class getData extends AsyncTask<Void, Void, String> {
 Dialog dialog;
String url;

@Override
   protected void onPreExecute() {
 super.onPreExecute();

 }

@Override
protected String doInBackground(Void... params) {
  url = "Enter your url";

 JSONParser jParser = new JSONParser();
 String json = jParser.getJSONFromUrl(url);
 try {
  JSONObject jobject = new JSONObject(json);

  int success = jobject.getInt("success");

  for (int i = 0; i < jobject.length() - 1; i++) {
   JSONObject jobj = jobject
   .getJSONObject(Integer.toString(i));
   if (success == 1) {
    HashMap<String, String> hm = new HashMap<String, String>();

    hm.put("p_name", jobj.getString("p_name"));
    hm.put("p_title", jobj.getString("p_title"));
    aldata.add(hm);
   }
  }
 } catch (JSONException e) {
  e.printStackTrace();
 }
   return null;

}

@Override
 protected void onPostExecute(String result) {
 super.onPostExecute(result);
 if (dialog != null)
  if (dialog.isShowing())
 dialog.dismiss();
 Custom_Adapter adapter = new Custom_Adapter(
 (Activity) MainActivity.this, aldata);
 lvdata.setAdapter(adapter);
}
 }

  }
  1. Custom Adapter 定制适配器

Custom_Adapter.java Custom_Adapter.java

public class Custom_Adapter extends ArrayAdapter<HashMap<String, String>> {

private final Activity context;
 private final LinkedList<HashMap<String, String>> allData;

public Custom_Adapter(Activity context,
 LinkedList<HashMap<String, String>> list) {
super(context, R.layout.custom_list, list);
 this.context = context;

this.allData = list;
}

static class ViewHolder {

TextView tvname, tvinfo;

}

@Override
 public View getView(int position, View convertView, ViewGroup parent) {
 View view = convertView;
view = null;
if (view == null) {
 LayoutInflater inflator = context.getLayoutInflater();
 view = inflator.inflate(R.layout.custom_list, null);
 final ViewHolder viewHolder = new ViewHolder();
 initAll(view, viewHolder);
 view.setTag(viewHolder);
 }
 ViewHolder holder = (ViewHolder) view.getTag();
 fillAll(holder, position);
return view;

  }

public void initAll(View view, ViewHolder viewHolder) {

viewHolder.tvname = (TextView) view.findViewById(R.id.tvname);

viewHolder.tvinfo = (TextView) view.findViewById(R.id.tvinfo);

}

public void fillAll(final ViewHolder holder, final int position) {

holder.tvname.setText(allData.get(position).get("p_name"));
holder.tvinfo.setText(allData.get(position).get("p_title"));

 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM