Bei klick auf ListView Adresse öffnen

C

-chriss-

Fortgeschrittenes Mitglied
6
Hallo,
ich habe zu Testzwecken diese HashMap-Liste:

Code:
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CustomListView extends Activity {
    
    // ArrayList which contains our HashMap's with different objects
    private  ArrayList<HashMap<String, Object>> myList;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
             
        // Create Hash Map
        myList = new ArrayList<HashMap<String, Object>>();
        
        // Fill data
        addDataToList();
        
        // View
        ListView mainListView = (ListView) findViewById(R.id.main_listview);
        
        // Adapter
        SimpleAdapter aa = new SimpleAdapter(this, myList, R.layout.row,
                new String[] {"name", "town"}, 
                new int[] {R.id.txt_name, R.id.txt_town});
        mainListView.setAdapter(aa);
        
        // Listener
        mainListView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              // Get the HashMap of the clicked item
              HashMap<String, Object> user = myList.get(position);
              
              // Get Attribute name of the HashMap
              String name = (String)user.get("name");
              String town = (String)user.get("town");
              StringBuffer linkXML = new StringBuffer();
              
              // Get Attribute name of the HashMap
              String name = (String)user.get("name");
              String town = (String)user.get("town");
              String url = (String)user.get("link");
                            

                    try {
                        URL urlXML = new URL(url);
                        InputStreamReader isr;
                        try {
                            isr = new InputStreamReader(urlXML.openStream());
                            BufferedReader in = new BufferedReader(isr);

                            String inputLine;

                            while ((inputLine = in.readLine()) != null){
                               linkXML.append(inputLine);
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        }
                    catch (MalformedURLException e) {
                        e.printStackTrace();
                    }

              [COLOR=Blue]// Create new Dialog
              final Dialog dialog = new Dialog(CustomListView.this);
              dialog.setTitle("Menü der " + name + town);
              TextView txtDescription = new TextView(CustomListView.this);
              txtDescription.setPadding(10, 0, 0, 10);
              txtDescription.setText("Detail Description");
              dialog.setContentView(txtDescription);[/COLOR]
              
              dialog.setCanceledOnTouchOutside(true);
              dialog.show();
          }
        });  
    }
    
    /**
     * This method is used, to fill data into our List
     */
    private void addDataToList(){
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        map1.put("name","Mensa ");
        map1.put("town","Furtwangen");
       map1.put("[COLOR=Lime]link[/COLOR]", "[COLOR=Red]http://www.studentenwerk.uni-freiburg.de/index.php?id=speiseplaene&no_cache=1&L=&Tag=0&Ort_ID=641[/COLOR]");
        
        HashMap<String, Object> map2 = new HashMap<String, Object>();    
        map2.put("name", "Mensa ");
        map2.put("town", "Trossingen");
       map2.put("[COLOR=Lime]link[/COLOR]", "[COLOR=Red]http://www.studentenwerk.uni-freiburg.de/index.php?id=speiseplaene&no_cache=1&L=&Tag=0&Ort_ID=672[/COLOR]");
// Der letzte Teil des Hashmapeintrages wird mit Absicht nicht angezeigt, sondern nur mitgegeben. 
        
        myList.add(map1);
        myList.add(map2);    
    }
}
Die row.xml:

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingBottom="10dp" android:paddingTop="10dp"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_height="wrap_content" android:gravity="center_vertical"
        android:layout_width="fill_parent">
        
    <TableLayout android:id="@+id/TableLayout01"
            android:layout_below="@+id/LinearLayout01" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:layout_marginLeft="10dp">
            <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:id="@+id/TextView01"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:textColor="#FFFFFF" android:paddingRight="10dp"
                    android:text="Name:"></TextView>
                <TextView android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:id="@+id/txt_name"
                    android:textColor="#FFFFFF" android:textStyle="bold"
                    android:paddingBottom="5dp"></TextView>
            </TableRow>
            <TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:textColor="#FFFFFF"
                    android:paddingRight="10dp" android:text="Town:" android:id="@+id/TextView02"></TextView>
                <TextView android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:textColor="#FFFFFF"
                    android:paddingBottom="5dp" android:id="@+id/txt_town"></TextView>
            </TableRow>
            
        </TableLayout>
    </LinearLayout>
</RelativeLayout>
Die main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/main_listview">
</ListView>
string.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ListView!</string>
    <string name="app_name">CustomListView</string>
    <string name="menu_title">Menü der</string>
</resources>
So nun hab ich die komplette Seite im String inputLine, das Problem es ist nichts geparst...dh. ich hab noch den ganzen xml und html code drin... den ich ja für die letztendliche Anzeige nicht brauche (will).

Alternativ kann ich auch mit der URL eine neue webView-Activity starten...zeigt aber dann die komplette Seite an...
 
Zuletzt bearbeitet:

Ähnliche Themen

A
Antworten
10
Aufrufe
1.017
swa00
swa00
C
Antworten
8
Aufrufe
1.122
swa00
swa00
D
Antworten
17
Aufrufe
395
datNeMo
D
Zurück
Oben Unten