3

我有一个带有 ListView 的应用程序,在任何行上单击它都会弹出一些不同的东西。(使用 PauseDialog)。并且任何行中的标题都在“array.xml”中,并且将在 PauseDialog 中弹出的文本在 java 代码中。问题是我需要向 ListView 添加很多行,我想知道是否有比我做的更有效的方法。array.xml 的示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <array name="Row_List">  
            <item>row1</item>
            <item>row2</item>
            <item>row3</item>
            <item>row4</item>
    </array>   
</resources>

(只是更多的行......)java代码:

public class MainActivity extends Activity implements OnItemClickListener {
    ListView list;
    ArrayAdapter<String> adaptr;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.listView1);
        adaptr = new ArrayAdapter<String>(this,
                 android.R.layout.simple_list_item_1
                         , getResources().getStringArray(R.array.Row_List));
            View header = getLayoutInflater().inflate(R.layout.header, null);
        list.addHeaderView(header);
        list.setAdapter(adaptr);
        list.setOnItemClickListener(this);
        list.setFastScrollEnabled(true);
    }
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Dialog dialog1 = new Dialog(this, R.style.PauseDialog);
        dialog1.setContentView(R.layout.dialog1);
        TextView text = (TextView) dialog1.findViewById(R.id.tv1);
        dialog1.setTitle(R.string.Title);
        dialog1.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        switch(position) {
        case 0:
            String url = "My-Site.com";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
            break;
        case 1:
            text.setText("Description of Row1");
            dialog1.show();
            break;
        case 2:
            text.setText("Description of Row2");
            dialog1.show();
            break;
        case 3:
            text.setText("Description of Row3");
            dialog1.show();
            break;
        case 4:
            text.setText("Description of Row4");
            dialog1.show();
            break;
        }
    }
}

那么有更好的方法将描述放入对话框中吗?(我想是否有办法将所有描述都放在 xml 文件中而不是代码中)谢谢!

4

1 回答 1

1

您可以将描述存储为string-arrayin arrays.xml

<string-array name="desciptions">
    <item>item 0 descript</item>
    <item>item 1 descript</item>
    <item>item 2 descript</item>
</string-array>

..定义一个数组MainActivity:

static String[] desciptions = null;

.. 在 onCreate() 中初始化它:

if (null==desciptions) desciptions
  = getResources().getStringArray(R.array.descriptions);

..并用这样的东西替换你的switch语句,考虑到你的'0'特殊情况:

if (0==position) {
    String url = "My-Site.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
} else {
    text.setText(descriptions[position]);
    dialog.show();
}

请记住,您需要为您的特殊0情况留出余地,以保持索引对齐,例如使用text.setText(descriptions[position-1]);或使用空的 item 0 描述。

于 2013-03-09T19:36:41.220 回答