原始文件夹中的多个txt文件|如何获取项目点击列表视图中的txt

时间:2015-11-18 03:36:04

标签: java android textview android-resources

我在 res / raw 文件夹中有多个文本文件。我想从列表视图中点击项目获取特定的文本文件。

b.java (包含列表数组和rawid的活动)。

public class b extends Activity{
ListView lv;
int count=0;
int[] rawid={R.raw.addamtopm,R.raw.factorial,R.raw.fibonacci};

String[] content={
        "Add AM/PM to time using SimpleDateFormat",
        " Add or substract minutes to current time",
        "Add or substract days to current date",
        "Add or substract hours to current time"

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b);
    lv=(ListView)findViewById(R.id.listView1);


    ArrayList<String> arrayList=new ArrayList<String>(Arrays.asList(content));
    ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,R.layout.text_design,R.id.textView,arrayList);
    lv.setAdapter(arrayAdapter);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,        int position, long id) {
            Intent intent = new Intent(b.this, b1.class);
            startActivity(intent);
        }
    });
}

b1.java

public class b1 extends b {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b1);
    TextView textView = (TextView) findViewById(R.id.textView2);
    textView.setText(readTextFile(b1.this,rawid[count]));
}

public static String readTextFile(Context ctx, int resId)
{
    InputStream inputStream = ctx.getResources().openRawResource(resId);
    InputStreamReader inputreader = new InputStreamReader(inputStream);
    BufferedReader bufferedreader = new BufferedReader(inputreader);
    String line;
    StringBuilder stringBuilder = new StringBuilder();
    try
    {
        while (( line = bufferedreader.readLine()) != null)
        {
            stringBuilder.append(line);
            stringBuilder.append('\n');
        }
    }
    catch (IOException e)
    {
        return null;
    }
    return stringBuilder.toString();
}

当我点击列表位置0或位置1时,位于R.raw.addamtopm的{​​{1}}被打开,我想根据位置获取txt文件项目点击列表视图。

B.XML

rawid[0]

b1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView1"
    />

我被困在这里请帮助我们。

1 个答案:

答案 0 :(得分:0)

您必须将位置值从第一个类(b.java)传递到第二个(b1.java)。

`

 Intent intent = new Intent(b.this, b1.class); intent.putextra("pos",position);
                startActivity(intent);

`

And just get value in b.java oncreate


Bundle extras = getIntent().getExtras(); 
        int position;
      if (extras != null) {
        position= extras.getint("pos");
        // and get whatever type user account id is
       }

然后写下每个位置的条件

if(position==0){
     textView.setText(readTextFile(b1.this,R.raw.addamtopm);
  }