我正在研究基於碎片的學校項目。我製作了XML文件,但在nearfragment.java中出現錯誤。你能告訴我錯誤在哪裏,因爲我熟悉Activity而不是Fragment。請幫我糾正錯誤。帶有片段的Android ListView
near_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="{relativePackage}.${activityClass}" >
<ListView
android:id="@+id/list_li"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
near_layout_mylist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon_list"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/item_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33" />
<TextView
android:id="@+id/textView1_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
CustomListNearAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListNearAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public CustomListNearAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.near_layout_mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.near_layout_mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item_list);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_list);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1_list);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
NearFragment
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListView;
/**
* Created by Ratan on 7/29/2015.
*/
public class NearFragment extends Fragment {
ListView list;
String[] itemname ={
"Safari",
"Camera",
"Global",
"FireFox",
"UC Browser",
"Android Folder",
"VLC Player",
"Cold War"
};
Integer[] imgid={
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
R.drawable.akash,
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.near_layout, container,false);
CustomListNearAdapter adapter=new CustomListNearAdapter(this, itemname, imgid);
ListView listView = (ListView) view.findViewById(R.id.list_item);
listView.setAdapter(new CustomListNearAdapter(getActivity()));
return view;
}
}
錯誤在於nearfragment,我無法編寫片段的代碼。所以請給我一些指導。
:Akashdeep Singh:什麼是錯誤?併發布logcat。 – kevz
而不是這個,爲什麼你不使用'CustomActivity()' –
'CustomListNearAdapter'你不應該使用構造函數的所有項目?活動,項目名稱和圖像? – KVISH