I am trying to populate a ListView from a .txt
file but somehow fail to it. The file read and is added to the ArrayList properly. I have tried the ArrayAdapter to the ArrayList and set it as the Adapter of the ListView under OnCreate()
and call notifyDataSetChanged()
after the list is updated.
I'm fairly new to java, I'm more used to (and prefer) C#
Here's parts of my code:
public class MainActivity extends ActionBarActivity{
ArrayAdapter<String> arrayAdapter = null;
ArrayList<String> arrayList = null;
ListView listView = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
listView = (ListView) findViewById(R.id.lv_Run);
arrayList = new ArrayList<String>();
arrayList.clear();
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
//<...>
if (file.isFile() && file.getName().endsWith(".txt")){
Button btn = new Button(ll1.getContext());
btn.setText(file.getName().replace(".txt", ""));
btn.setTag(file.getName().replace(".txt", ""));
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String btnString = v.getTag().toString();
UpdateList(btnString);
}
});
btn.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
ll1.addView(btn);
}
//<...>
public void UpdateList(String btnString){
try{
File file = new File(runs.getAbsolutePath()+ File.separator + btnString + ".txt");
arrayList = getFileContent(file.getAbsolutePath());
arrayAdapter.notifyDataSetChanged();
catch (IOException e){
e.printStackTrace();
}
}
//<...>
public static ArrayList<String> getFileContent(String fileName) throws IOException{
ArrayList<String> result = new ArrayList<>();
File aFile = new File(fileName);
BufferedReader reader;
String aLine;
if (!aFile.isFile()){
return result;
}
try{
reader = new BufferedReader(new FileReader(aFile));
}
catch (FileNotFoundException e1){
e1.printStackTrace();
return result;
}
while ((aLine = reader.readLine()) != null){
result.add(aLine + "\n");
}
reader.close();
return result;
}
//<...>
<LinearLayout 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=".MainActivity"
android:background="#ff000000"
android:baselineAligned="false"
android:id="@+id/pan_MasterPan">
<LinearLayout android:orientation="vertical"
android:layout_weight="2.5"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/pan_Left">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:layout_weight="8"
android:id="@+id/pan_SelRun">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/select_run"
android:id="@+id/btn_LoadRun"
android:clickable="true"
android:onClick="runSelectClick"/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="1"
android:focusableInTouchMode="false"
android:id="@+id/pan_RunPOI">
<ListView
android:id="@+id/lv_Run"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="visible"
android:layout_weight="1"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:clickable="true"
android:choiceMode="singleChoice"
android:divider="@android:color/black"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ff1A1A1A"
android:id="@+id/pan_Right">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ForDebug"
android:text="@string/debugbtnstrg"
android:clickable="true"
android:onClick="ForDebug_OnClick"
android:longClickable="false"
android:textStyle="italic"/>
</LinearLayout>
</LinearLayout>
Two days I try this and that without any difference between every try...
Thanks a lot for you time, it's really appreciated.
---- Edit ----
Updated Code: (sorry to show only parts, this is part of a massive and messy code...)
public void UpdateList(String btnString){
try{
File file = new File(runs.getAbsolutePath()+ File.separator + btnString + ".txt");
arrayList = getFileContent(file.getAbsolutePath());
Toast.makeText(getApplicationContext(), "aL_Size: " + arrayList.size(), Toast.LENGTH_LONG).show();
//return 5
arrayAdapter.addAll(arrayList);
Toast.makeText(getApplicationContext(), "aA_Count: " + arrayAdapter.getCount(), Toast.LENGTH_LONG).show();
//return 5
arrayAdapter.notifyDataSetChanged();
catch (IOException e){
e.printStackTrace();
}
}
---- Edit 2 ----
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
listView = (ListView) findViewById(R.id.lv_Run);
arrayList = new ArrayList<String>();
arrayList.clear();
arrayList.add("TEST1");
arrayList.add("TEST2");
arrayList.add("TEST3");
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
Call ArrayAdapter.addAll method for adding new items in current adapter of ListView before calling notifyDataSetChanged
:
arrayList = getFileContent(file.getAbsolutePath());
arrayAdapter.addAll(arrayList);
arrayAdapter.notifyDataSetChanged();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다