您好我有一个列表,我尝试从中settext
,例如我知道TextView.setText(List<String>object)
像TextView.setText(array[index])
但它不起作用。我尝试了也得到了。是的实现自定义适配器的正确方法:
我应该设置文本列表,然后我将添加图像,等等。
以下是我的代码:
public class GetName extends Fragment {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
//ArrayAdapter<String> adapter;
CustomList adapter;
public GetName()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Parse.initialize(getActivity(), "user", "pass");
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
View view=inflater.inflate(R.layout.getnewlay,container, false);
new GetData().execute();
return view;
}
private class GetData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
ParseQuery query = new ParseQuery("Name");
query.orderByDescending("_created_at");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listview = (ListView)getView().findViewById(R.id.listviewget);
adapter = new CustomList(getActivity(), ob);
/*ArrayAdapter<String>(getActivity(),
R.layout.listview_item);*/
// Retrieve object "name" from Parse.com database
for (ParseObject country : ob) {
adapter.add((String) country.get("name"));
}
listview.setAdapter(adapter);
}
}
}
适配器类:
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] descr;
private final Integer[] imageId;
private final Integer[] imagId;*/
private List<ParseObject> ob;
public CustomList(Activity context, List<ParseObject> ob) {
super(context, R.layout.list_single);
this.ob = ob;
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.lname);
>for(ParseObject i : ob) txtTitle.setText((CharSequence) i);
> txtTitle.setText( ob.get(position));
return rowView;
}
}
答案 0 :(得分:5)
使用此
TextView.setText(String.valueOf(array[index]));
答案 1 :(得分:3)
你的对象应该是字符串。例如,
TextView.setText("Your Text");
只能工作。
所以试试,
TextView.setText(array[index].toString())
或
TextView.setText(String.valueOf(array[index]))
我希望这会对你有所帮助。
答案 2 :(得分:3)
textView.setText(array[index].toString());
或者只是
textView.setText(array[index]+"");
答案 3 :(得分:2)
只需你可以这样做:
TextView.setText(String.valueOf(array[index]));
答案 4 :(得分:2)
您需要从
更改此设置txtTitle.setText(ob.get(position));
到
txtTitle.setText(ob.get(position).get("name"));