۱۳۹۴/۰۴/۲۸, ۱۰:۰۰ ب.ظ
من این برنامه رو نوشتم ولی با مسشکل مواجه شدم و اینکه در کلاس سرچ اطلاعات رو دریافت میکنم حالا چه طوری داخل یه لیست ویو نمایش بده البته اکتیویتی مربوط به کلاس سرچ حاوی یه سری تکس باکس هست و نمیتون لیست ویو رو داخل اون نمایش بدم الان باید چیکار کنم؟[i][i][i][i] [/i][/i][/i][/i]
کد پیاچپی:
package library_iaun.ac.ir;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import library_iaun.ac.ir.R.id;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class search extends Activity {
EditText etonvan,etmozoe,etnasher,etpadidavar;
Button search,newsearch;
String onvan,mozoe,nasher,padidavar;
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
private static final String json_name = "book";
private static final String json_node_name_1 = "id";
private static final String json_node_name_2 = "onvan";
private static final String json_node_name_3 = "mozoe";
private static final String json_node_name_4 = "nasher";
private static final String json_node_name_5 = "padidavar";
JSONArray book = null;
public String app_id_string;
public String json_string_1_all;
public String json_string_2_all;
public String json_string_3_all;
public String json_string_4_all;
public String json_string_5_all;
public int json_length;
public ListView lv;
//URL to get JSON Array
//JSON Node Names
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
etonvan=(EditText)findViewById(id.et_onvan);
etmozoe=(EditText)findViewById(id.et_mozoe);
etnasher=(EditText)findViewById(id.et_nasher);
etpadidavar=(EditText)findViewById(id.et_padidavar);
search=(Button)findViewById(id.btn_search);
search.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
onvan=etonvan.getText().toString().trim();
mozoe=etmozoe.getText().toString().trim();
nasher=etnasher.getText().toString().trim();
padidavar=etpadidavar.getText().toString().trim();
new JSONParse().execute();
}
});
}
public class JSONParse extends AsyncTask<String, String, JSONObject> {
public ProgressDialog pDialog;
@Override
public void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(search.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
public JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(link.search(onvan, mozoe, nasher, padidavar));
return json;
}
@Override
public void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// kelidestan
book = json.getJSONArray(json_name);
// build String
json_length = book.length();
json_string_1_all = new String [book.length()];
json_string_2_all = new String [book.length()];
json_string_3_all = new String [book.length()];
json_string_4_all = new String [book.length()];
json_string_5_all = new String [book.length()];
for(int i = 0; i < book.length(); i++){
JSONObject c = book.getJSONObject(i);
// Storing JSON item in a Variable --> with UTF-8 for persian words
json_string_1_all[i] = new String(c.getString(json_node_name_1).getBytes("ISO-8859-1"), "UTF-8");
json_string_2_all[i] = new String(c.getString(json_node_name_2).getBytes("ISO-8859-1"), "UTF-8");
json_string_3_all[i] = new String(c.getString(json_node_name_3).getBytes("ISO-8859-1"), "UTF-8");
json_string_4_all[i] = new String(c.getString(json_node_name_4).getBytes("ISO-8859-1"), "UTF-8");
json_string_5_all[i] = new String(c.getString(json_node_name_5).getBytes("ISO-8859-1"), "UTF-8");
}
// show data in ListView
setListAdapter(new MyAdapter(search.this,
R.layout.list_layout,
R.id.textView1aaaa,
json_string_1_all));
lv = getListView();
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
private void setListAdapter(MyAdapter myAdapter) {
// TODO Auto-generated method stub
}
private ListView getListView() {
// TODO Auto-generated method stub
return null;
}
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String id = json_string_1_all[position];
TextView tv_1 = (TextView) row.findViewById(R.id.textView1aaaa);
tv_1.setText(id);
String onvan = json_string_2_all[position];
TextView tv_2 = (TextView) row.findViewById(R.id.textView2aaaa);
tv_2.setText(onvan);
String mozoe = json_string_3_all[position];
TextView tv_3 = (TextView) row.findViewById(R.id.textView3aaaa);
tv_3.setText(mozoe);
String nasher = json_string_3_all[position];
TextView tv_4 = (TextView) row.findViewById(R.id.textView4aaaa);
tv_4.setText(nasher);
String padidavar = json_string_3_all[position];
TextView tv_5 = (TextView) row.findViewById(R.id.textView5aaaa);
tv_5.setText(padidavar);
return row;
}
}
}
[/i][/i][/i][/i][/i]