Buka Eclipse buat project baru, pada folder res/layout, activity_main.xml biarkan saja. Buat file baru dengan nama list_item_title_navigation.xml , lalu pastekan koding berikut ini.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ImageView android:id="@+id/imgIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
android:layout_marginRight="5dp"
/>
<TextView android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/imgIcon"/>
</RelativeLayout>
Pada folder src/namapackage, buat file baru dengan nama SpinnerNavItem.java pastekan koding dibawah ini.
package com.spinner;
public class SpinnerNavItem {
private String title;
private int icon;
public SpinnerNavItem(String title, int icon){
this.title = title;
this.icon = icon;
}
public String getTitle(){
return this.title;
}
public int getIcon(){
return this.icon;
}
}
Buat file baru dengan nama StringParser.java, pastekan koding berikut ini.
package com.spinner;
import java.util.ArrayList;
public class StringParser
{
public StringParser()
{
}
public ArrayList<Object> Parse(String input)
{
ArrayList<Object> output = new ArrayList<Object>();
int pointer = 0;
String data = "";
while (pointer <= input.length()-1)
{
if (input.charAt(pointer) == '#')
{
output.add(data);
data = "";
pointer++;
}
if (pointer < input.length())
{
data+= input.charAt(pointer);
}
pointer++;
}
return output;
}
}
Buat file baru dengan nama TitleNavigationAdapter.java, pastekan koding berikut ini.
package com.spinner;
import java.util.ArrayList;
import android.app.Activity;
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;
public class TitleNavigationAdapter extends BaseAdapter {
private ImageView imgIcon;
private TextView txtTitle;
private ArrayList<SpinnerNavItem> spinnerNavItem;
private Context context;
public TitleNavigationAdapter(Context context,
ArrayList<SpinnerNavItem> spinnerNavItem) {
this.spinnerNavItem = spinnerNavItem;
this.context = context;
}
@Override
public int getCount() {
return spinnerNavItem.size();
}
@Override
public Object getItem(int index) {
return spinnerNavItem.get(index);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
imgIcon.setVisibility(View.GONE);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item_title_navigation, null);
}
imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
txtTitle.setText(spinnerNavItem.get(position).getTitle());
return convertView;
}
}
ubah file MainActivity.java seperti koding dibawah ini.
package com.spinner;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity implements OnNavigationListener {
private ActionBar actionBar;
private ArrayList<SpinnerNavItem> navSpinner;
private TitleNavigationAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
cek();
}
private void cek() {
actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
navSpinner = new ArrayList<SpinnerNavItem>();
String urlll = "http://10.0.2.2/test/loads.php";
HttpClient httpclient = new DefaultHttpClient();
HttpRequestBase httpRequest = null;
HttpResponse httpResponse = null;
InputStream inputStream = null;
String response = "";
StringBuffer buffer = new StringBuffer();
httpRequest = new HttpGet(urlll);
try {
httpResponse = httpclient.execute(httpRequest);
} catch (ClientProtocolException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
inputStream = httpResponse.getEntity().getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = new byte[512];
int len = 0;
try {
while (-1 != (len = inputStream.read(data))) {
buffer.append(new String(data, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
response = buffer.toString();
StringParser parser = new StringParser();
ArrayList<Object> output = parser.Parse(response);
Object[] Output = output.toArray();
String[] content = new String[Output.length];
// content[0] = "semua";
Log.e("JJJJJ", Output.length + "=" + content.length);
for (int i = 0; i < content.length; i++) {
navSpinner.add(new SpinnerNavItem(Output[i].toString(),
R.drawable.ic_launcher));
}
adapter = new TitleNavigationAdapter(getApplicationContext(),
navSpinner);
actionBar.setListNavigationCallbacks(adapter, this);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
SpinnerNavItem s = navSpinner.get(itemPosition);
Toast.makeText(getApplicationContext(), s.getTitle().toString(),
Toast.LENGTH_SHORT).show();
return false;
}
}
Jalankan maka hasilnya seperti berikut.
No comments:
Post a Comment