ListView Example using php MySQL.
listview_example.php
<?php
define('HOST','mysql.hostinger.in');
define('USER','u532517283_user');
define('PASS','your_password ');
define('DB','u532517283_bdm');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from ListViewExample";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('item_id'=>$row[0],
'item_name'=>$row[1],
'image'=>'http://compliments.esy.es/images/uploads/'.$row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
bulid.gradle
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.squareup.picasso:picasso:2.4.0'
compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")
compile 'com.squareup.picasso:picasso:2.4.0'
compile files("${android.getSdkDirectory().getAbsolutePath()}" + File.separator + "platforms" + File.separator + "android-23" + File.separator + "optional" + File.separator + "org.apache.http.legacy.jar")
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.listvieweaxmple"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NextPage"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> </application> </manifest>
MainActivity.java
package com.listvieweaxmple; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.JsonHttpResponseHandler; import org.apache.http.Header; import org.apache.http.entity.StringEntity; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ArrayList<class_items> mArrayList; ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mArrayList = new ArrayList<class_items>(); mListView = (ListView) findViewById(R.id.listview); get(); } private void get() { AsyncHttpClient client = new AsyncHttpClient(); client.addHeader("Accept", "application/json"); StringEntity mStringEntity = null; client.post(this, "http://compliments.esy.es/listview_example.php", mStringEntity, "application/json", new JsonHttpResponseHandler() { ProgressDialog mProgressDialog; @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { super.onSuccess(statusCode, headers, response); try { JSONArray mJsonArray = response.getJSONArray("result"); JSONObject object; for (int i = 0; i < mJsonArray.length(); i++) { object = mJsonArray.getJSONObject(i); mArrayList.add(new class_items( object.getString("item_id"), object.getString("item_name"), object.getString("image"))); } mListView.setAdapter(new List_View_Adaptor(MainActivity.this, mArrayList)); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { Toast.makeText(MainActivity.this, "Data Connection error", Toast.LENGTH_SHORT).show(); super.onFailure(statusCode, headers, throwable, errorResponse); } @Override public void onStart() { super.onStart(); mProgressDialog = ProgressDialog.show(MainActivity.this, "Loading", "Please Wait", true, false); } @Override public void onFinish() { super.onFinish(); if (mProgressDialog.isShowing()) { mProgressDialog.dismiss(); } } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { class_items items = (class_items) parent.getItemAtPosition(position); Intent intent=new Intent(MainActivity.this,NextPage.class); intent.putExtra("item_name",items.item_name); intent.putExtra("image",items.image); startActivity(intent); } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // app icon in action bar clicked; goto parent activity. this.finish(); return true; default: return super.onOptionsItemSelected(item); } } }
List_View_Adaptor.java
package com.listvieweaxmple; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.squareup.picasso.Picasso; import java.util.ArrayList; public class List_View_Adaptor extends BaseAdapter { Context mContext; ArrayList<class_items> mArrayList; LayoutInflater mLayoutInflater; public List_View_Adaptor(Context mContext, ArrayList<class_items> mArrayList) { this.mContext = mContext; this.mArrayList = mArrayList; this.mLayoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return mArrayList.size(); } @Override public Object getItem(int position) { return mArrayList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { list_items_view item; if(convertView==null) { convertView=mLayoutInflater.inflate(R.layout.listview_item,null); item=new list_items_view((ImageView)convertView.findViewById(R.id.imageView), (TextView)convertView.findViewById(R.id.textView)); convertView.setTag(item); } else { item=(list_items_view)convertView.getTag(); } String str=mArrayList.get(position).image; Picasso.with(mContext).load(str).into(item.getImg1()); item.getTv1().setText(mArrayList.get(position).getItem_name()); return convertView; } }
class_items.java
package com.listvieweaxmple; public class class_items { public String image; public String item_id; public String item_name; public class_items(String item_id, String item_name, String image) { this.item_id = item_id; this.item_name = item_name; this.image = image; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getItem_id() { return item_id; } public void setItem_id(String item_id) { this.item_id = item_id; } public String getItem_name() { return item_name; } }
list_items_view.java
package com.listvieweaxmple; import android.widget.ImageView; import android.widget.TextView; public class list_items_view{ ImageView img1; TextView tv1; public list_items_view(ImageView img1, TextView tv1) { this.img1 = img1; this.tv1 = tv1; } public ImageView getImg1() { return img1; } public void setImg1(ImageView img1) { this.img1 = img1; } public TextView getTv1() { return tv1; } public void setTv1(TextView tv1) { this.tv1 = tv1; } }
NextPage.java
package com.listvieweaxmple; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; import com.squareup.picasso.Picasso; public class NextPage extends AppCompatActivity { String item_name,image; TextView item_name_txt; ImageView image_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next_page); Intent i = getIntent(); item_name= i.getStringExtra("item_name"); image = i.getStringExtra("image"); item_name_txt = (TextView)findViewById(R.id.item_name_txt); item_name_txt.setText(item_name); image_view =(ImageView)findViewById(R.id.image_view); Picasso.with(this).load(image).into(image_view); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>
Listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Frame1"> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/imageView" android:background="#80383838" android:layout_margin="10dp" android:scaleType="fitXY"/> <ImageView android:layout_width="match_parent" android:layout_height="101dp" android:id="@+id/imageView1" android:layout_margin="10dp" android:scaleType="fitXY" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/textView" android:layout_gravity="center" android:hint="title" android:gravity="center_vertical|center|center_horizontal" android:typeface="serif" android:textColor="#000000" android:textSize="25dp" android:layout_margin="10dp" /> </FrameLayout> </RelativeLayout>
Activity_next_page.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_next_page" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.listvieweaxmple.NextPage" android:gravity="center"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/image_view"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/item_name_txt" android:hint="item name" android:textSize="30sp" android:gravity="center" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>