jsonarray to multy textView


jsonarray to multy textView

我从一个php文件中创建了一个代码来获取这样的jsonarray

{name:alex,family:alexx},{name:john,family:Doe}and ...

我将它们转换为字符串在HOMe_activity中使用以下代码:

public class home extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    SharedPreferences pref = getApplicationContext().getSharedPreferences("userPref", 0);
    SharedPreferences.Editor editor = pref.edit();
    String useridnow = pref.getString("userid", null);
    if(useridnow == null) {
        Intent i = new Intent(home.this,MainActivity.class);
       home.this.startActivity(i);
    }
    new Getfeed(home.this,useridnow).execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}
class Getfeed extends AsyncTask<Void, Void, String> {
private String userid;
private Context context;
RelativeLayout rellay;
public Getfeed(Context context, String userid) {
    this.userid = userid;
    this.context = context;
}
@Override
protected String doInBackground(Void... voids) {
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://fb.facenegah.com/android/showfeed.php");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
        nameValuePairs.add(new BasicNameValuePair("feedid","0"));
        nameValuePairs.add(new BasicNameValuePair("limit", "1"));
        nameValuePairs.add(new BasicNameValuePair("userid", userid));
        nameValuePairs.add(new BasicNameValuePair("uper", "0"));
        nameValuePairs.add(new BasicNameValuePair("downer", "0"));
        nameValuePairs.add(new BasicNameValuePair("isprofile", "0"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        String responseText = EntityUtils.toString(entity);
        return responseText;
    }
    catch(Exception ex)
    {
    }
    return null;
}
@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    JSONObject jsonResponse;
   //Toast.makeText(context, "Json: "+s, Toast.LENGTH_SHORT).show();
    try {
        ArrayList<String> temp = new ArrayList<String>();
        jsonResponse = new JSONObject(s);
        JSONArray movies = jsonResponse.getJSONArray("salam");
        for(int i=0;i<movies.length();i++){
            JSONObject movie = movies.getJSONObject(i);
            String characters = movie.getString("user_esm");
            Toast.makeText(context, "Json: "+characters , Toast.LENGTH_LONG).show();
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.rowlayout, null);
            View rowView2 = (LinearLayout) rowView.findViewById(R.id.rowlay);
            View homeview =  inflater.inflate(R.layout.activity_home, null);
            View homeview2 =  (LinearLayout) homeview.findViewById(R.id.homelay);
            TextView textView = (TextView) rowView.findViewById(R.id.textView3);
            ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
            textView.setText(characters);
            //setListAdapter(rowView);
            //this line give me an error for addView with no suggestion in adroid studio
            rowView2.addView(rowView2);


        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

我有自定义布局,只有两个文本视图:

<TextView
android:layout_width="wrap_content"
android:layout_height="124dp"
android:text="New Text"
android:id="@+id/textView3"
android:layout_weight="0.24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="124dp"
android:text="New Text"
android:id="@+id/textView4"
android:layout_weight="0.24" />

我只是不知道在jsonarray存在的时候告诉我的代码
它将值放入此自定义布局中的textView中
并在用户打开HOme活动时重复显示
就像探戈新闻源

如果您想将自定义布局(现在由JSON文件填充)添加到主布局中,您可以这样做:

给正在添加自定义布局的布局一个id。例如,我给了我的main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/main"
...

在您的代码中获得对主布局的引用,如下所示:

RelativeLayout main = (RelativeLayout) findViewById(R.id.main);

然后,在创建每个自定义布局后,就像您在for循环中所做的那样,将rowView添加到main:

main.addView(rowView);

我不知道Tango newfeed是什么样子的,但如果你正在创建类似newfeed的东西,你可能想使用某种适配器,而不是手动添加(然后删除?)布局。例如,您可以将ListView放在xml中,然后使用ListAdapter来填充数据。使用列表适配器意味着android将处理滚动,并在滚动屏幕时自动重用视图以提高性能。