我試圖訪問列表視圖中存在的按鈕。在按鈕上,按鈕的值應該變成「你好」Android:無法訪問存在於片段內的ListView中的按鈕
listview是片段的一部分。
適配器接受圖像,文件名和文件創建日期的值。 隨着三個元素有一個按鈕(我將用它來刪除文件)
但我無法訪問按鈕。
public class FragmentC extends Fragment implements AdapterView.OnItemClickListener{
ListView list;
public FragmentC() {
// Required empty public constructor
}
String[] days;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String path = Environment.getExternalStorageDirectory().toString()+"/Notate";
File f = new File(path);
File file[] = f.listFiles();
days=new String[file.length];
Integer[] imageId=new Integer[file.length];
String[] dateTime=new String[file.length];
for (int i=0; i < file.length; i++)
{
String temp=file[i].getName();
String temp2=temp.substring(0,temp.length()-4);
Date lastModDate = new Date(file[i].lastModified());
days[i]=temp2;
imageId[i]=R.drawable.ic_launcher;
dateTime[i]=lastModDate.toString().substring(0,lastModDate.toString().length()-14);
}
final View contextView = inflater.inflate(R.layout.fragment_fragment_c,container,false);
CustomList adapter = new
CustomList(this.getActivity(), days, imageId,dateTime);
list=(ListView) contextView.findViewById(R.id.listView);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
return contextView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Button b1=(Button) view.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
b1.setText("Hello");
}
});
}
}
非常感謝!這工作!我會很快將這個標記爲正確的答案! – shivram
盡情享受吧.. –