1

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. I'm completely new to Android and obviously a bit overwhelmed. 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:

<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):

<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:

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.

1
  • @Override public int getCount(){ return 0; }... why?? Commented Oct 24, 2014 at 15:47

3 Answers 3

3

Your ListView is not showing nothing because of

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

getCount has to return the number of elements that belong to your dataset. For instance

 @Override
    public int getCount(){
        return text1.length;
    }  
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. That was fast. Thanks a lot. In fact I ignored this method because it's not being called anywhere. Is this internally called? How does it work?
1

You're returning 0 from getCount. Return the number of items in the list here, I suspect you want something like text1.length.

Comments

0

Hey i see my simple and best example, Follow some steps

I mostly use custom for testing

JSON Parsing Step 1.

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.

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

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"));

 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.