有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java jsonarray到multy textView

我从一个php文件中创建了这样的get-jsonarray代码

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

我在HOMe_活动中将它们转换为字符串,代码如下:

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/安卓/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
安卓:layout_width="wrap_content"
安卓:layout_height="124dp"
安卓:text="New Text"
安卓:id="@+id/textView3"
安卓:layout_weight="0.24" />
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="124dp"
安卓:text="New Text"
安卓:id="@+id/textView4"
安卓:layout_weight="0.24" />

我只是不知道在jsonarray存在时告诉我的代码 将其值放入此自定义布局的文本视图中
并在用户打开家庭活动时重复显示它们
就像探戈新闻源一样


共 (1) 个答案

  1. # 1 楼答案

    如果您想将自定义布局(现在由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的东西,你可能想使用某种适配器,而不是手动添加(然后删除?)布局。例如,可以在xml中放置ListView,然后使用ListAdapter填充数据。使用列表适配器意味着android将处理滚动,并在视图滚动屏幕时自动重用视图,以提高性能