• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

[Ask] how to make list menu from database

n3wbie

Lurker
i've problem here...

i just make a simple project with android...i make some connection with my database in my host....so it's online...i can view all my records with a simple textview. from here i just want to take the string value on my database and make it as menu list. this is my source hope someone would help me...please :)

the database content is username


package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class testing extends ListActivity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
cek();
}

public void cek(){

String address = "http//mywebsite/select.php";

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(address);

ArrayList<NameValuePair> parameter = new ArrayList<NameValuePair>();

try {
httppost.setEntity(new UrlEncodedFormEntity(parameter));
HttpResponse httpResponse = httpclient.execute(httppost);
HttpEntity httpentity = httpResponse.getEntity();

InputStream input = httpentity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(input));

String content = "";
String line = "";

while((line = read.readLine())!=null){
content += line;

}

Log.d("ADBUG", "content: "+content);

if(!content.equals("null")){

try {
JSONArray jArr = new JSONArray(content);
String[] user = new String[jArr.length()];
for(int i=0;i<jArr.length();i++){
JSONObject jObj = jArr.getJSONObject(i);
String username = jObj.getString("username");
user=username;

}


this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, user));

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "not yet : " + keyword, Toast.LENGTH_LONG).show();

}
}
 
Back
Top Bottom