如何保存文件并使其与单击的列表视图项具有相同的名称

时间:2012-03-06 06:05:19

标签: android arrays listview save

我有这个代码填充listview但我还需要知道如何用填充listview的相同数组中的字符串填充textview,我需要这个的原因是我有一个代码将文件保存到内部或外部记忆,但我不知道如何设置名称,然后在那里写它我想要从填充列表的相同数组设置它,例如listview是故事一,故事二,依此类推,当他们点击一个故事一我希望保存文件的名称与他们点击的listitem名称相同.....这里是我的代码

    public class MainActivity extends ListActivity {

String[] list = null;
String[] listITEMS = list;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);  
setContentView(R.layout.menu_list);

TextView textview = (TextView) findViewById(R.id.textView1);
textview.setText(listITEMS[0]);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getItems());
setListAdapter(adapter);

ListView storyListDownload = getListView();
storyListDownload.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        if  (position == 0) {
            Intent buttona = new Intent(view.getContext(), StoryOne.class);
            startActivity(buttona);
        }


    }
});

}

private List<String> getItems() {
List<String> list = new ArrayList<String>();
JSONArray jArray = connectToServer("http://10.0.2.2/textures_story_list.php");
JSONObject json_data = null;
for(int i=0; i < jArray.length(); i++) {
    try {
        json_data = jArray.getJSONObject(i);
        list.add(json_data.getString("story_name"));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
return list;
}

public static JSONArray connectToServer(String address) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(address);
try {           
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF8"),8);
    StringBuilder sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");
    String line="0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    String result = sb.toString();
    JSONArray array = new JSONArray(result);
    return array;
} catch(Exception e){
    //Log.e("log_tag", "Error converting result "+e.toString());
    return null;
}
}   
}

我已经知道如何保存文件但不知道如何给它起一个名字,然后自己编写....而且文件扩展需要是.json以及,我希望我可以使用工作listvoew但填写具有相同名称atm的textview 任何帮助将不胜感激

0 个答案:

没有答案