首页 > 解决方案 > 如何从 .t​​xt 文件填充自定义列表视图?安卓工作室

问题描述

尝试从 .txt 文件填充自定义列表视图。

自定义列表视图有 2 行,主标题和副标题。

这就是我尝试过的:

在 MainActivity.java 中:

public void readTxt() {

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/file.txt";
        File file = new File( path );
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br2 = new BufferedReader( new FileReader( file ) );
            String line;

            while ((line = br2.readLine()) != null) {

                    String[] maintitle = {
                            line
                    };

                    String[] subtitle = {
                            line
                    };

                    MyListAdapter adapter = new MyListAdapter( this, maintitle, subtitle );
                    lv = (ListView) findViewById( R.id.lv );
                    lv.setAdapter( adapter );

                }

          br2.close();
        } catch (IOException e) {
            Toast.makeText( this, "Error loading playlist", Toast.LENGTH_SHORT ).show();
        }
    }
}

在 MyListAdapter.java 中:

import android.app.Activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] maintitle;
    private final String[] subtitle;


    public MyListAdapter(Activity context, String[] maintitle, String[] subtitle) {

       super(context, R.layout.lv_player, maintitle);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.maintitle=maintitle;
        this.subtitle=subtitle;

    }

    public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.lv_player, null,true);

        TextView titleText = (TextView) rowView.findViewById(R.id.maintitle);
        TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);

        titleText.setText(maintitle[position]);
        subtitleText.setText(subtitle[position]);

        return rowView;

    };
}

我的 .txt 文件如下所示:Apple,think different,诺基亚,connecting people,Huawei,Make it possible

使用此代码仅填充第一项,如何填充所有?

标签: androidfilelistviewtextpopulate

解决方案


您是否尝试过向您的适配器添加一个方法来设置内容并从您的文件中传递所有信息?

它可能看起来像这样

适配器:

...
 public MyListAdapter(Activity context) {
       super(context, R.layout.lv_player, maintitle);

       this.context=context;
    }
... 
public void setContent (String[] titleFromAfile, String [] subtitleFromAfile){
       maintitle = titleFromAfile
       subtitle = subtitleFromAfile
}

活动:

while (){
...
}

MyListAdapter adapter = new MyListAdapter( this);
                    lv = (ListView) findViewById( R.id.lv );
                    lv.setAdapter( adapter )

adapter.setContent(mainTitle, subTitle);

不要忘记附加数组


推荐阅读