【发布时间】:2015-01-28 13:42:09
【问题描述】:
大家好,我有这个片段创建方法
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
user_id = sp.getString("user_id", "anon");
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_friends_list, null, false);
listview = (ListView)view.findViewById(R.id.friends_list);
fadapter = new FriendsAdapter(getActivity(), R.layout.single_friend);
listview.setAdapter(fadapter);
listview.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
fadapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listview.setSelection(fadapter.getCount() - 1);
}
});
我在日志中看到我的数据提供者正在创建列表项 这个类在我的 fragment.java 中
class GetFriends extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id));
Log.w("params", params.toString());
Log.d("request!", "starting");
// getting product details by making HTTP request
jsonArray = jsonParser.makeHttpRequest(
EVENTS_URL, "POST", params);
Log.w("jsonArray", jsonArray.toString());
if (jsonArray != null) {
try {
for (int i = 0; i < jsonArray.length(); i++) {
Log.w("jsonArray", jsonArray.getJSONObject(i).toString());
//CREATE ITEM FOR LISTVIEW
fadapter.add(new FriendsProvider(
jsonArray.getJSONObject(i).getInt("id"),
jsonArray.getJSONObject(i).getString("username")));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
但我的片段列表没有显示任何内容。
片段列表视图:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myup2.up2.FriendsFragment">
<ListView
android:id="@+id/friends_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff" />
单身朋友 - 列表项
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/singleFriend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:paddingLeft="10dip"
android:textColor="@android:color/primary_text_light"
android:background="@drawable/own_event_bubble"
android:text="hello worldvfvdfvdfvdfv"
/>
适配器
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.single_friend,parent,false);
}
friends_item = (TextView)convertView.findViewById(R.id.singleFriend);
FriendsProvider provider = getItem(position);
friends_item.setText(provider.username );
return convertView;
}
也许我正在考虑将适配器和提供程序复杂化。 但它可以在应用程序的其他部分工作。
但也许我必须刷新片段的 UI 或类似的东西?
编辑 1:
public class FriendsAdapter extends ArrayAdapter<FriendsProvider>{
private List<FriendsProvider> friends_list = new ArrayList<FriendsProvider>();
private TextView friends_item;
Context ctx;
public FriendsAdapter(Context context, int resource) {
super(context, resource);
ctx = context;
}
@Override
public void add(FriendsProvider object) {
friends_list.add(object);
super.add(object);
}
@Override
public int getCount() {
return friends_list.size();
}
@Override
public FriendsProvider getItem(int position) {
return friends_list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
{
LayoutInflater inflator = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.single_friend,parent,false);
}
friends_item = (TextView)convertView.findViewById(R.id.singleFriend);
FriendsProvider provider = getItem(position);
friends_item.setText(provider.username );
return convertView;
}
}
【问题讨论】:
标签: android mysql android-fragments android-arrayadapter