Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert listview to gridview

below is my code which works fine for showing listview horizontally. how can I change it to gridvew. What changes should I make to change it to gridview? help me please

public class fifthscreen extends Activity {

int IOConnect = 0;

String _response;
String status;

HorizontalListView listview;
CategoryListAdapter3 cla;

String URL, URL2;
String SelectMenuAPI;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
public static String allergen2;

String name;

String url1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fifthscreen);

    listview = (HorizontalListView) this.findViewById(R.id.listview2);

    cla = new CategoryListAdapter3(fifthscreen.this);
    new TheTask().execute();
}

public class TheTask extends AsyncTask<Void, String, String> {

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

    }

    @Override
    protected String doInBackground(Void... arg0) {

        try {


            HttpGet request = new HttpGet(url);
            HttpResponse response = client.execute(request);
            HttpEntity resEntity = response.getEntity();
            _response = EntityUtils.toString(resEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return _response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {

            JSONObject json2 = new JSONObject(result);

            status = json2.getString("status");
            if (status.equals("1")) {

                JSONArray school4 = json2.getJSONArray("dish_allergen");
                //
                for (int i = 0; i < school4.length(); i++) {
                    JSONObject object = school4.getJSONObject(i);

                    Category_ID.add((long) i);
                    Category_name.add(object.getString("name"));
                    Category_image.add(object.getString("image"));

                }

            }

            else {

                JSONArray school2 = json2.getJSONArray("data");
                for (int i = 0; i < school2.length(); i++) {
                    JSONObject object = school2.getJSONObject(i);

                    Category_ID.add((long) i);
                    Category_name.add(object.getString("name"));
                }
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        listview.setAdapter(cla);
    }
}
     }







   public class CategoryListAdapter3 extends BaseAdapter {

private Activity activity;

private AQuery androidAQuery;


public CategoryListAdapter3(Activity act) {
    this.activity = act;
//  imageLoader = new ImageLoader(act);
}

public int getCount() {
    // TODO Auto-generated method stub
    return fifthscreen.Category_ID.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;
    androidAQuery = new AQuery(getcontext());
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.viewitem2, null);
        holder = new ViewHolder();

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }


    holder.txtText = (TextView) convertView.findViewById(R.id.title2);
    holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2);

    holder.txtText.setText(fifthscreen.Category_name.get(position));
    //  imageLoader.DisplayImage(fifthscreen.Category_image.get(position),   
          activity, holder.imgThumb);


     androidAQuery.id(holder.imgThumb).image(fifthscreen.Category_image.get(position), false, 
     false);

    return convertView;
}
private Activity getcontext() {
    // TODO Auto-generated method stub
    return null;
}
static class ViewHolder {
    TextView txtText;
    ImageView imgThumb;
}

          }



             <!---  fifithscreen.xml--->

                  <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
          android:layout_marginLeft="10dp"
          android:layout_marginRight="10dp"
          android:layout_below="@+id/test_button_text5"
         android:orientation="vertical" >

   <com.example.examplecode.HorizontalListView
           android:id="@+id/listview2"
           android:layout_width="wrap_content"
           android:layout_height="120dp"
             android:layout_below="@+id/test_button_text5"

           android:background="#ffffff"/>


      </LinearLayout>




   <!--viewitem2.xml--->



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

   >

  <ImageView
android:id="@+id/image2"
   android:layout_width="90dp"
 android:layout_height="70dp"
  android:scaleType="fitXY"
 android:padding="5dp"

  android:layout_marginLeft="5dp"
   android:layout_marginRight="5dp"

android:src="@drawable/ic_launcher"
/>

<TextView
android:id="@+id/title2"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textColor="#000"
android:paddingTop="10dp"


android:gravity="center_horizontal"
/>



   </LinearLayout>
like image 477
user2857293 Avatar asked Oct 08 '13 08:10

user2857293


1 Answers

No change at all. Just set adapter of GridView as your are setting for ListView.

like image 106
Khawar Raza Avatar answered Sep 28 '22 00:09

Khawar Raza