gpt4 book ai didi

android - 如何在带有列android的ListView中显示对象数组?

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:59 25 4
gpt4 key购买 nike

我开发混合 android 应用程序,我现在正在学习原生 android。我想在 native android 中创建一个简单的 TableView 。现在在 ionic 中我这样做。

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);
}

我怎样才能做得更简单,就像一个有列的表格?谢谢!

最佳答案

在我的主要 Activity 中,我有如下静态响应

   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;
}

}

您好,我已经使用 Adapter 完成了,您可以在此处获得完整教程 ListView with multiple columns

关于android - 如何在带有列android的ListView中显示对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com