如何在更改JSON中的数据后更新列表视图?

时间:2015-01-12 10:42:09

标签: android json

大家好,新年快乐

我有JSON类,我在其中检索从数据库检索的一些数据。此JSON文件的格式为

{"people":[{"id":"15","first_name":"Theo","last_name":"Tziomakas","bio":"Hello from 
Theo!!!","created":"2015-01-11 21:48:51"},
{"id":"16","first_name":"Jim","last_name":"Chytas","bio":"Hello from Chytas","created":"2015-01-11 
21:53:42"}]}.

想法是在列表视图中检索“first_name”和“second_name”。 “生物”应出现在另一项活动中,但我不知道该怎么做:(。

这是我的代码。

 public class MainActivity extends ActionBarActivity {
 ListView list;
 TextView fname;
TextView lname;


 ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
//URL to get JSON Array
private static String url = "http://xxxxxxx/tutorials/index.php";
//JSON Node Names
private static final String TAG_OS = "people";
private static final String TAG_FIRST_NAME = "first_name";
private static final String TAG_SECOND_NAME = "last_name";
//private static final String TAG_BIO = "bio";
JSONArray android = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    oslist = new ArrayList<HashMap<String, String>>();


            new JSONParse().execute();
        }


private class JSONParse extends AsyncTask<String, String, JSONObject> {
    private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        fname = (TextView)findViewById(R.id.first_name);
        lname = (TextView)findViewById(R.id.last_name);
        //abio = (TextView)findViewById(R.id.bio);

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        return json;
    }
    @Override
    protected void onPostExecute(JSONObject json) {
        pDialog.dismiss();
        try {
            // Getting JSON Array from URL
            android = json.getJSONArray(TAG_OS);
            for(int i = 0; i < android.length(); i++){
                JSONObject c = android.getJSONObject(i);
                // Storing  JSON item in a Variable
                String first_name = c.getString(TAG_FIRST_NAME);
                String last_name = c.getString(TAG_SECOND_NAME);
                //String bio = c.getString(TAG_BIO);
                // Adding value HashMap key => value
                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_FIRST_NAME, first_name);
                map.put(TAG_SECOND_NAME, last_name);
                //map.put(TAG_BIO, bio);
                oslist.add(map);
                list=(ListView)findViewById(R.id.list);
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
                        R.id.first_name,R.id.last_name});
                list.setAdapter(adapter);
                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        String bio = ((TextView) view.findViewById(R.id.bio))
                                .getText().toString();
                        switch (position) {
                            case 0:

                            Intent i = new Intent(MainActivity.this, SingleActivity.class);
                            i.putExtra("bio", bio);
                            startActivity(i);
                            break;
                        }
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
     }
  }
}

最后我有了SecondActivity类,但是当我点击第一行列表时没有显示任何内容。

 public class SingleActivity extends ActionBarActivity {
 TextView text;
 JSONArray android = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single);

    text = (TextView)findViewById(R.id.text);
    String bio = getIntent().getStringExtra("bio");

    try {
        JSONObject profileJSON = new JSONObject(bio);
        android = profileJSON.getJSONArray(bio);
        text.setText(""+android);
    } catch (JSONException e) {
        e.printStackTrace();
    }


  }
}

谢谢。

编辑:我遇到的问题是固定的。现在我想做点别的事。让我们假设我们通过更改“first_name”,“last_name”和最终生物来更新现有行中的数据。在phpmyadmin工具中很容易完成。问题是更新的行没有显示在列表视图中。我必须重新安装应用程序以便查看它。有任何想法吗?

2 个答案:

答案 0 :(得分:1)

试试这个,

用此

替换onPostExecute
 protected void onPostExecute(JSONObject json) {
   pDialog.dismiss();
   try {
       list=(ListView)findViewById(R.id.list);
       // Getting JSON Array from URL
       android = json.getJSONArray(TAG_OS);
       for(int i = 0; i < android.length(); i++){
           JSONObject c = android.getJSONObject(i);
           // Storing  JSON item in a Variable
           String first_name = c.getString(TAG_FIRST_NAME);
           String last_name = c.getString(TAG_SECOND_NAME);
           //String bio = c.getString(TAG_BIO);
           // Adding value HashMap key => value
           HashMap<String, String> map = new HashMap<String, String>();
           map.put(TAG_FIRST_NAME, first_name);
           map.put(TAG_SECOND_NAME, last_name);
           map.put(TAG_BIO, bio);
           oslist.add(map);


       }

       ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
               R.layout.list_v,
               new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
               R.id.first_name,R.id.last_name});
       list.setAdapter(adapter);
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                                   int position, long id) {
               String bio =oslist.get(position).get(TAG_BIO);
              // switch (position) {
                 //  case 0:

                   Intent i = new Intent(MainActivity.this, SingleActivity.class);
                   i.putExtra("bio", bio);
                   startActivity(i);
                   break;
               //}
           }
       });
   } catch (JSONException e) {
       e.printStackTrace();
   }
}

和SingleActivity

text = (TextView)findViewById(R.id.text);
String bio = getIntent().getStringExtra("bio");
text.setText(bio );

答案 1 :(得分:0)

注意你对意图的投入。

public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    String bio = ((TextView) view.findViewById(R.id.bio))
                            .getText().toString();
                    switch (position) {
                        case 0:

                        Intent i = new Intent(MainActivity.this, SingleActivity.class);
                        i.putExtra("bio", bio);
                        startActivity(i);
                        break;
                    }
                }

请注意,您从文本视图中获取文本并将其添加到and intent。

当你进行检索时,就像使用json对象一样使用它。 如果你想要生物字符串,你可以把它放在一个变量中,然后把它发送到只有字符串的意图。

例如

ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                   R.layout.list_v,
                    new String[] { TAG_FIRST_NAME,TAG_SECOND_NAME }, new int[] {
                    R.id.first_name,R.id.last_name});
            list.setAdapter(adapter);
            String bio = c.getString("bio");
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {

                    switch (position) {
                        case 0:

                        Intent i = new Intent(MainActivity.this, SingleActivity.class);
                        i.putExtra("bio", bio);
                        startActivity(i);
                        break;
                    }
                }
            });

检索时,使用

更容易实现
public class SingleActivity extends ActionBarActivity {
    TextView text;
    JSONArray android = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);

        text = (TextView)findViewById(R.id.text);
        String bio = getIntent().getStringExtra("bio");

        text.settext(bio)


        }
    }

现在这些例子中存在一些错误,因为我的计算机上没有安装java或android sdk