如何设置点击项目,以进行其他布局?

时间:2018-09-24 09:30:25

标签: android listview

如何设置On单击项目,以进行其他布局?

enter image description here

Public class DataParser extends AsyncTask<Void,Void,Boolean> {

 Context c;
 String jsonData;
 ListView lv;

 ProgressDialog pd;
 ArrayList<String> spacecrafts=new ArrayList<>();

 public DataParser(Context c, String jsonData, ListView lv) {
    this.c = c;
    this.jsonData = jsonData;
    this.lv = lv;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    pd=new ProgressDialog(c);
    pd.setTitle("Parse");
    pd.setMessage("Pasring..Please wait");
    pd.show();
 }

 @Override
 protected Boolean doInBackground(Void... params) {
     return this.parseData();
 }

@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);

    pd.dismiss();
    if(result)
    {
        ArrayAdapter adapter=new 
    ArrayAdapter(c,android.R.layout.simple_list_item_1,spacecrafts);
            lv.setAdapter(adapter);

      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int 
  position, long id) {
                 Toast.makeText(c, spacecrafts.get(position), 
   Toast.LENGTH_SHORT).show();
            }
        });
    }
   }

   private Boolean parseData()
   {
       try
      {
          JSONArray ja = new JSONArray(jsonData);
          JSONObject jo;

        spacecrafts.clear();

        for (int i = 0; i < ja.length(); i++) {

            jo = ja.getJSONObject(i);


            String name = jo.getString("name");

             spacecrafts.add(name);
         }

         return true;


      } catch (JSONException e) {
        e.printStackTrace();
      }
      return false;
     }
  }

0 个答案:

没有答案