我开发混合Android应用程序,我现在正在学习本机android。我想在原生android中创建一个简单的表视图。现在在离子中我这样做。
JS
$scope.get_users = function(){
$http.post('192.168.0.132/api/get_users', {is_active:true})
.success(function(data){
$scope.users = data;
}.error(function(Error_message){
alert(Error_message)
}
}
HTML
<table>
<thead>
<tr>
<th>Name</th>
<th>Role</th>
</tr>
<tbody>
<tr ng-repeat="user in users">
<td ng-bind="::user.name"></td>
<td ng-bind="::user.role"></td>
<td><button ng-click="select_user(user)"></td>
</tr>
</tbody>
</table>
简单吧?一切都已确定。
现在在android中这是我的代码。
public void get_users(){
HttpURLConnection urlConnection;
int length = 5000;
try {
// Prepare http post
URL url = new URL("http://192.168.0.132/api/get_users");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "");
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// initiate http post call
urlConnection.connect();
// Get input stream
InputStream is = urlConnection.getInputStream();
// Convert Stream to String
String contentAsString = convertInputStreamToString(is, length);
// Initialize Array
JSONArray jObj = new JSONArray(contentAsString);
int length = jObj.length();
List<String> listContents = new ArrayList<String>(length);
// Convert JSON Array to
for(int i=0; i<jObj.length(); i++){
listContents.add(jObj.getString(i).toString());
}
// Set the array to listview adapter and set adapter to list view
ListView myListView = (ListView) findViewById(R.id.listView2);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listContents);
myListView.setAdapter(arrayAdapter);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[length];
reader.read(buffer);
return new String(buffer);
}
我怎么能这样做更简单,就像一个有列的表?谢谢!
答案 0 :(得分:4)
在我的主要活动中,我有如下静态响应
package com.example.listcolumn;
import static com.example.listcolumn.Constant.FIRST_COLUMN;
import static com.example.listcolumn.Constant.SECOND_COLUMN;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
private ArrayList<HashMap> list;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lview = (ListView) findViewById(R.id.listview);
populateList();
listviewAdapter adapter = new listviewAdapter(this, list);
lview.setAdapter(adapter);
}
private void populateList() {
list = new ArrayList<HashMap>();
HashMap temp = new HashMap();
temp.put(FIRST_COLUMN, "Karthik");
temp.put(SECOND_COLUMN, "Developer");
list.add(temp);
HashMap temp1 = new HashMap();
temp1.put(FIRST_COLUMN, "Hello");
temp1.put(SECOND_COLUMN, "Developer");
list.add(temp1);
HashMap temp2 = new HashMap();
temp2.put(FIRST_COLUMN, "We r fr u");
temp2.put(SECOND_COLUMN, "Deeveloper");
list.add(temp2);
HashMap temp3 = new HashMap();
temp3.put(FIRST_COLUMN, "Hoffensoft");
temp3.put(SECOND_COLUMN, "Company");
list.add(temp3);
}
}
使用适配器设置值
package com.example.listcolumn;
import static com.example.listcolumn.Constant.FIRST_COLUMN;
import static com.example.listcolumn.Constant.FOURTH_COLUMN;
import static com.example.listcolumn.Constant.SECOND_COLUMN;
import static com.example.listcolumn.Constant.THIRD_COLUMN;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class listviewAdapter extends BaseAdapter {
public ArrayList<HashMap> list;
Activity activity;
public listviewAdapter(Activity activity, ArrayList<HashMap> list) {
super();
this.activity = activity;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtFirst;
TextView txtSecond;
TextView txtThird;
TextView txtFourth;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_main, null);
holder = new ViewHolder();
holder.txtFirst = (TextView) convertView.findViewById(R.id.FirstText);
holder.txtSecond = (TextView) convertView.findViewById(R.id.SecondText);
holder.txtThird = (TextView) convertView.findViewById(R.id.ThirdText);
holder.txtFourth = (TextView) convertView.findViewById(R.id.FourthText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
HashMap map = list.get(position);
holder.txtFirst.setText((CharSequence) map.get(FIRST_COLUMN));
holder.txtSecond.setText((CharSequence) map.get(SECOND_COLUMN));
holder.txtThird.setText((CharSequence) map.get(THIRD_COLUMN));
holder.txtFourth.setText((CharSequence) map.get(FOURTH_COLUMN));
return convertView;
}
}
您好我已经完成了使用适配器和完整的教程,您可以到这里ListView with multiple columns