0

私は次のようなAndroidアプリに取り組んでいます:

(ListView開始)

名前-ゲーム

日にち

リンク(ボタン)


Name2-Game2

日付2

Link2(ボタン)

(ListView終了)

それはListViewにあります。したがって、ListView要素は次のとおりです。名前、ゲーム、日付(TextView)、およびリンク(ボタン)すべてのデータはXMLファイルから取得されます

だから私の質問:ボタンのリンクを取得するにはどうすればよいですか?XMLファイルからListViewElement1のボタンへのlink1など。すでにいくつかのチュートリアルをチェックしましたが、私のコードで動作させることができません:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        String xml = XMLfunctions.getXML();
        Document doc = XMLfunctions.XMLfromString(xml);

        int numResults = XMLfunctions.numResults(doc);

        if((numResults <= 0)){
            Toast.makeText(Main.this, "...", Toast.LENGTH_LONG).show();  
            finish();
        }

        NodeList nodes = doc.getElementsByTagName("result");

        for (int i = 0; i < nodes.getLength(); i++) {                            
            HashMap<String, String> map = new HashMap<String, String>();    

            Element e = (Element)nodes.item(i);
            if(i==0){
                TextView update= (TextView) findViewById(R.id.textView6);
                update.setText("last Update: "+XMLfunctions.getValue(e, "update"));
            } else {

            map.put("name", XMLfunctions.getValue(e, "name")+": " + XMLfunctions.getValue(e, "game") );
            map.put("date", XMLfunctions.getValue(e, "date"));
             mylist.add(map);    
            }    
        }        
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "date" }, 
                        new int[] { R.id.item_title, R.id.item_date });

        setListAdapter(adapter);

        final ListView lv = getListView();        
        lv.setTextFilterEnabled(true);    

編集

私が解析するxmlの例:

<result>
<id>1</id>
<name>test</name>
<game>gamename</game>
<date>27.04</date>
<link>www.google.com</link>
</result>
<result>
<id>2</id>
<name>test2</name>
<game>gamename2</game>
<date>27.04</date>
<link>www.google.com</link>
</result>

およびリストプレースホルダーのレイアウト

<ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="280dp"
    android:drawSelectorOnTop="false" android:scrollbars="horizontal|vertical"/>

およびメインレイアウト(ListViewのレイアウト)

<TableLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/item_title"
                android:layout_width="146dp"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="10dp" />



        </TableRow>
         <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/item_date"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="10dp" />
            </TableRow>



        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <Button
                android:id="@+id/buttonlink"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="10dp" android:layout_height="fill_parent"
                android:text="Link"
                 />





        </TableRow>

    </TableLayout>
4

1 に答える 1

0

新しい独自のアダプターを追加したところ、動作するので、誰かが探している場合:

public class DataAdapter extends ArrayAdapter<Data>   
        {

            private ArrayList<Daten> items;
            String url;

            public DatenAdapter(Context context, int textViewResourceId, ArrayList<Daten> items) {
                    super(context, textViewResourceId, items);
                    this.items = items;
            }
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                    View v = convertView;
                    if (v == null) {
                        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.list_element, null);
                    }
                    Daten o = items.get(position);

                    url = o.getUrl();
                    if (o != null) {

                            TextView name = (TextView) v.findViewById(R.id.item_title);
                            TextView datum = (TextView) v.findViewById(R.id.item_date);
                            if (name != null) {
                                  name.setText(o.getName()+" - "+o.getSpiel());                            }
                            if(datum != null){
                                  datum.setText("date: "+ o.getDate());

                            }
                    }
                    Button but = (Button) v.findViewById(R.id.buttonlink);

                    but.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View view) {
                        openWebURL(getUrl(position));
                        }
                    });



                    return v;

            }



            private String getUrl(int index) {
                return d.get(index).getUrl();
            }
于 2012-04-27T19:36:59.757 に答える