2

I have loaded data to normal list view from JSON array like this

 JSONArray jArray = new JSONArray(result);       
 final String[] array_spinner = new String[jArray.length()];
 for(int i=0;i<jArray.length();i++){
     JSONObject json_data = jArray.getJSONObject(i);
     String jj=json_data.getString("f_name");
     array_spinner[i] = jj;
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,  android.R.layout.simple_list_item_1,array_spinner); adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
 //adapter.setDropDownViewResource(R.layout.spinner_layout);
 list.setAdapter(adapter);

I want to load the data to custom listview. my XML file for custom listview is this receiver.xml

How can I load data to custom listview from JSON?

1 Answer 1

7
  • Create a custom Adapter extending BaseAdpter or ArrayAdpter and pass array or ArrayList in the constructor
  • Create the View in a layout (of row)
  • Inflate this xml in getView function of the custom Adapter and set the data

as in

http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/

Populate Listview from JSON

for you i mixed both to make you understand...

Activity XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lstText"
    />
</LinearLayout>

list row XML (in layout row.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">

        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtAlertText" />

    </LinearLayout>
</LinearLayout>

you adapter class

class JSONAdapter extends BaseAdapter implements ListAdapter {

    private final Activity activity;
    private final JSONArray jsonArray;
    private JSONAdapter (Activity activity, JSONArray jsonArray) {
        assert activity != null;
        assert jsonArray != null;

        this.jsonArray = jsonArray;
        this.activity = activity;
    }


    @Override public int getCount() {
        if(null==jsonArray) 
         return 0;
        else
        return jsonArray.length();
    }

    @Override public JSONObject getItem(int position) {
         if(null==jsonArray) return null;
         else
           return jsonArray.optJSONObject(position);
    }

    @Override public long getItemId(int position) {
        JSONObject jsonObject = getItem(position);

        return jsonObject.optLong("id");
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = activity.getLayoutInflater().inflate(R.layout.row, null);



        TextView text =(TextView)convertView.findViewById(R.id.txtAlertText);

                    JSONObject json_data = getItem(position);  
                    if(null!=json_data ){
                    String jj=json_data.getString("f_name");
                    text.setText(jj); 
                   }

         return convertView;
    }
}

your activty

public class main extends Activity {
    /** Called when the activity is first created. */

    ListView lstTest;
    //Array Adapter that will hold our ArrayList and display the items on the ListView
    JSONAdapter jSONAdapter ;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Initialize ListView
        lstTest= (ListView)findViewById(R.id.lstText);


        jSONAdapter = new JSONAdapter (main.this,jArray);//jArray is your json array 

        //Set the above adapter as the adapter of choice for our list
        lstTest.setAdapter(jSONAdapter );


}
Sign up to request clarification or add additional context in comments.

7 Comments

I dont understand it dont you hav a simple example?
But it gives errors can you edit my code as i wished in the question?
jSONAdapter = new JSONAdapter (main.this, R.layout.listitems,<you JSON array >); here pases 3 parameters but in the constructor of the JSONADapter class has 2 parameters here: private VenuesAdapter(Activity activity, JSONArray jsonArray) {
VenuesAdapter ? what is that constructor? if I giv you my full code can you fix them?
I dont understand the code.Im new in android. Can you fix the project if i giv u the full code?
|

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.