私はこのアクティビティをデータベースのテーブルから取得し、このデータをListView内に表示します。しかし、私はこのListViewの中でクリック可能にしたいと思っています。ユーザーが項目をクリックすると、この項目の詳細を表示する別の画面にリダイレクトする必要があります。注文番号と日付だけを表示していますが、クリックすると詳細が表示されます。それ、どうやったら出来るの?データベース内のデータをListView内に表示
はここで、これまでに私のコードです:
public class Orders extends Fragment {
private String jsonResult;
//Here I get the order id and date
private String url = "https://www.example.com/orders.php";
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.orders, container, false);
listView = (ListView) v.findViewById(R.id.listView1);
TextView textView3 = (TextView) v.findViewById(R.id.textView16);
//Getting current date and displaying inside the textView
String currentDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()).format(new Date());
textView3.setText(Html.fromHtml(currentDate));
accessWebService();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
String[] parts = item.split(" ");
String order_nbr = parts[0];
String order_id = order_nbr.replaceAll("[#]","");
Intent intent = new Intent(getActivity(), OrderDetails.class);
intent.putExtra("order_id", order_id);
System.out.println(order_id);
startActivity(intent);
}
});
return v;
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("order_id",MainScreen.USERID);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(url,data);
jsonResult = result;
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("orders");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String order_number = jsonChildNode.optString("orderid");
String order_dateNhour = jsonChildNode.optString("order_date");
String[] parts = order_dateNhour.split(" ");
String date = parts[0];
String hour = parts[1];
String[] test1 = date.split("-");
String year = test1[0];
String month = test1[1];
String day = test1[2];
String outPut = "#" + order_number + " " + day+"/"+ month +"/"+ year + " " + hour + " >";
employeeList.add(createEmployee("orders", outPut));
}
} catch (JSONException e) {
Toast.makeText(getActivity().getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
android.R.layout.simple_list_item_1,
new String[]{"orders"}, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
問題はなんですか?あなたのコードで項目のクリックをうまく処理しているようです。アイテムがクリックされると、新しいアクティビティが開始されます。だから問題は何ですか?どのようにあなたのために働いていないのですか? – NoChinDeluxe
はい、その通りです。しかし、私はデータベースから注文IDと日付のみを取得し、それをリストビューで表示しています。リストビューでアイテムをクリックした後、他のスクリーン/アクティビティに表示するための注文の詳細を取得します。 –
あなたが提供したい情報であなたの意図にもっと多くのエキストラを渡してください。まだ問題は見られません。 – NoChinDeluxe