我从php文件
创建了一个类似于get 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的自定义布局:
<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活动上 就像Tango新闻源一样
答案 0 :(得分:0)
如果您要求将自定义布局(现在从JSON文件填充)添加到主布局,您可以执行以下操作:
为您添加自定义布局的布局添加ID。例如,我给了我<nav class="navbar navbar-default" style="border-radius: 0;">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed burger-button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand jura-font" href="/#!/">COLIGN</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<!-- <button type="submit" class="btn btn-default">Submit</button> -->
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-comment"></span></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-user"></span></a>
<ul class="dropdown-menu">
<li><a href="#">View Profile</a></li>
<li><a href="#">Manage Account</a></li>
<li><a href="#">Invite a friend</a></li>
<li role="separator" class="divider"></li>
<li><a href="/auth/signout">Log out</a></li>
</ul>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
。
main
在代码中获取对代码中主要布局的引用,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
...
然后,在创建每个自定义布局后,就像在for循环中一样,将RelativeLayout main = (RelativeLayout) findViewById(R.id.main);
添加到rowView
:
main
我不知道Tango newfeed的样子,但如果你正在创建类似newfeed的东西,你可能想要使用某种适配器而不是手动添加(然后删除?)布局。例如,您可以在xml中放置ListView,然后使用ListAdapter填充数据。使用列表适配器意味着android将执行诸如滚动之类的操作,并在滚动屏幕时自动重用视图以提高性能。