I want some help with putting data from multiple text files into one table shown on an activity.
The text file has a value on each line.
I want these strings to be shown on one column, in a table, where each column shows another text files data of similar format.
I want something like this to be shown:
The code im currently using shows one text file in a textview, how can i manipulate the code to make what i need possible?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
File sdCard = Environment.getExternalStorageDirectory();
String path = sdCard.getAbsolutePath() + "/SO/";
File file = new File(path + "cheststats.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
}
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(text);
}
}
This is not a good question, because all trivial information that you need for doing this are on Android Developers. But anyway, I hope following steps help you:
1) Create stats.xml layout for stats table:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableStats"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Stats"
android:gravity="center"
android:id="#+id/textView4" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Text File 1 Data"
android:id="#+id/textView1" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Text File 2 Data"
android:id="#+id/textView2" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Text File 3 Data"
android:id="#+id/textView3" />
</TableRow>
</TableLayout>
2) Create stats_row.xml layout for stats table rows:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|end" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|end" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|end" />
</TableRow>
3) Next, create function for reading file:
private List<String> readFileData(String filePath) {
List<String> values = new ArrayList<>();
try {
BufferedReader fileBufferedReader = new BufferedReader(new FileReader(new File(filePath)));
String value;
while ((value = fileBufferedReader.readLine()) != null) {
values.add(value);
}
fileBufferedReader.close();
} catch (IOException ignore) {
}
return values;
}
4) Create function for filling stats row view:
private void fillView(View view, String dataset1Value, String dataset2Value, String dataset3Value) {
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
TextView textView2 = (TextView) view.findViewById(R.id.textView2);
TextView textView3 = (TextView) view.findViewById(R.id.textView3);
textView1.setText(dataset1Value);
textView2.setText(dataset2Value);
textView3.setText(dataset3Value);
}
5) Finally your onCreate function should be like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
TableLayout tableStats = (TableLayout) findViewById(R.id.tableStats);
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SO/";
List<String> data1 = readFileData(path + "file1.txt");
List<String> data2 = readFileData(path + "file2.txt");
List<String> data3 = readFileData(path + "file3.txt");
int maxDataSetSize = Math.max(data1.size(), Math.max(data2.size(), data3.size()));
for (int i = 0; i < maxDataSetSize; i++) {
String dataset1Value = data1.size() > i ? data1.get(i) : null;
String dataset2Value = data2.size() > i ? data2.get(i) : null;
String dataset3Value = data3.size() > i ? data3.get(i) : null;
View statsRowview = getLayoutInflater().inflate(R.layout.stats_row, null);
fillView(statsRowview, dataset1Value, dataset2Value, dataset3Value);
tableStats.addView(statsRowview);
}
}
I'm not sure that it will work at the first attempt. I have not tested.
Related
Brand new to Android programming and just dinking around.
I've built a ListView that is populating posts. I've successfully loaded a JSON and can populate my custom layout with title, post content, and timestamp.
The problem is I can't figure out how to add tags (akin to the screenshot below of Zite's app, eg "Bosnia", "Visualization", "Security"). I have up to three tags that I want to add (location, school, friend) so I put three buttons in the layout. I can change the button text with the incoming JSON of course, but not every item has all three tags (for instance one item has only location, while another has location & school). So I want buttons to only appear when I have text for them, and I'll use gravity to force them left.
My first thought was a nested listview. The internet told me that is bad. Despite my searching, I found no tutorials or advice. But this feels like a known problem, any suggestions for learning resources?
Below is my relevant XML and java.
<TextView
android:id="#+id/feed_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="20dp"
android:text="#string/fTitle"
/>
<TextView
android:id="#+id/feed_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="2dp"
android:layout_below="#+id/feed_title"
android:textSize="15dp"
android:text="#string/fContent"
/>
<TextView
android:id="#+id/feed_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="2dp"
android:layout_below="#+id/feed_content"
android:textSize="10dp"
android:text="#string/fTime"/>
<ImageView
android:id="#+id/feed_like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginRight="25dp"
android:layout_below="#+id/feed_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="#+id/feed_time"
android:src="#drawable/ic_launcher"
/>
<ImageView
android:id="#+id/feed_flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/feed_content"
android:src="#drawable/ic_launcher"
android:layout_alignBottom="#+id/feed_like"
android:layout_toLeftOf="#+id/feed_like" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left"
android:layout_below="#+id/feed_time"
>
<Button
android:id="#+id/feed_tag_Locale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="#string/fTagLocal"
android:textSize="15dp"
/>
<Button
android:id="#+id/feed_tag_Network"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fTagNetwork"
android:textSize="15dp"
/>
<Button
android:id="#+id/feed_tag_Friend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fTagFriend"
android:textSize="15dp"
/>
</LinearLayout>
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null) {
view = mInflater.inflate(R.layout.row_feed, null);
holder = new ViewHolder();
holder.titleTextView = (TextView) view.findViewById(R.id.feed_title);
holder.postTextView = (TextView) view.findViewById(R.id.feed_content);
holder.timeTextView = (TextView) view.findViewById(R.id.feed_time);
holder.localeButton = (Button) view.findViewById(R.id.feed_tag_Locale);
holder.networkButton = (Button) view.findViewById(R.id.feed_tag_Network);
holder.friendButton = (Button) view.findViewById(R.id.feed_tag_Friend);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
JSONObject jsonObject = (JSONObject) getItem(i);
JSONObject subObject;
JSONObject traitsObject;
String postTitle = "";
String postContent = "";
String postTimestamp = "";
String postLocale = "";
String postNetwork = "";
String postFriend = "";
if (jsonObject.has("postID")) {
//postTitle = jsonObject.optString("title");
postTimestamp = jsonObject.optString("timePosted");
try {
subObject = jsonObject.getJSONObject("Content");
postTitle = subObject.optString("title");
postContent = subObject.optString("content");
traitsObject = jsonObject.getJSONObject("Poster");
postLocale = traitsObject.optString("location");
postNetwork = traitsObject.optString("network");
postFriend = traitsObject.optString("friends");
} catch (JSONException e) {
e.printStackTrace();
}
}
//if (jsonObject.has(""))
holder.titleTextView.setText(postTitle);
holder.postTextView.setText("I see you " + postContent);
holder.timeTextView.setText(postTimestamp);
holder.localeButton.setText(postLocale);
holder.networkButton.setText(postNetwork);
holder.friendButton.setText(postFriend);
return view;
}
At the end of getView, change this:
holder.localeButton.setText(postLocale);
holder.networkButton.setText(postNetwork);
holder.friendButton.setText(postFriend);
to this:
if(!postLocale.matches("")){
holder.localeButton.setText(postLocale);
}
else{
holder.localeButton.setVisibility(View.GONE);
}
if(!postNetwork.matches("")){
holder.networkButton.setText(postNetwork);
}
else{
holder.networkButton.setVisibility(View.GONE);
}
if(!postFriend.matches("")){
holder.friendButton.setText(postFriend);
}
else{
holder.friendButton.setVisibility(View.GONE);
}
In my project, im going to make a multiple choice type question. I can select the questions and answer from mysql, however, i cant do the "matching" of the answer between input answer and the data from mysql. I tried some solutions but they doesnt work as well. I need a big help on it. Below are my codes.
the java class that make multiple choice
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normalmode);
new DataAsyncTask().execute();
}
public void onClick(View v){
inputtext = ((Button) v).getText().toString();
tv3 = (TextView)findViewById(R.id.textView3);
tv3.setText(inputtext);
if(inputtext == tv2.getText().toString()){
playerscore +=5;
} else {
playerscore +=1;
}
score.setText("Scores : " + playerscore);
new DataAsyncTask().execute();
}
class DataAsyncTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... param) {
String result = DBConnector.executeQuery("SELECT * FROM questions ORDER BY RAND( ) LIMIT 1");
return result;
}
#Override
protected void onPostExecute(String result) {
question = (TextView)findViewById(R.id.textView_question);
fchoice = (Button)findViewById(R.id.Btn_choice1);
schoice = (Button)findViewById(R.id.Btn_choice2);
tchoice = (Button)findViewById(R.id.Btn_choice3);
tv2 = (TextView)findViewById(R.id.textView2);
score = (TextView)findViewById(R.id.textView_score);
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonData = jsonArray.getJSONObject(0);
question.setText(jsonData.getString("q_desc"));
fchoice.setText(jsonData.getString("fchoice"));
schoice.setText(jsonData.getString("schoice"));
tchoice.setText(jsonData.getString("tchoice"));
ans = jsonData.getString("q_ans");
tv2.setText(ans);
} catch(Exception e) {
Log.e("log_tag", e.toString());
}
}
}
}
three buttons are sharing same onClick in xml file by using
android:onClick="onClick"
the current problem i faced is that it always return "false" no matter i pressed which button. also, is there any way to store/pass "previous" async task data?
I have tried using internal storage but it doesnt work as well
EDIT:
my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/player_score" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/string_question"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Btn_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/third_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/second_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_choice2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/first_choice"
android:onClick="onClick" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
</LinearLayout
textview2 and textview3 are used to test my output and display them as text
try this if(inputtext.equals(tv2.getText().toString())) instead of if(inputtext == tv2.getText().toString())
becasue the == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
I am trying get the weather details like temp and condition and I am unable to Display them But I am able to see them in my log cat code.
Here is my code follows
cond = (TextView) findViewById(R.id.condDescr);
temp = (TextView) findViewById(R.id.tempText);
temp.setText("HI How are you");
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city});
private class JSONWeatherTask extends AsyncTask<String, Void, Weather> {
#Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0]));
try {
weather = JSONWeatherParser.getWeather(data);
// Let's retrieve the icon
//weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
#Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
if (weather.iconData != null && weather.iconData.length > 0) {
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
//imgView.setImageBitmap(img);
}
String str1 = (weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");**(I have tried this one too)**
String str2 = ("" + weather.currentCondition.getHumidity() + "%");
Log.w("myApp", str1);
Log.w("mytemp", str2);
cond.setText(str1);
temp.setText(str2);
//temp.setText("" + Math.round((weather.temperature.getTemp() - 273.15)) + "�C");
}
My XML File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/but_wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#000000"
android:onClick="onButtonClick"
android:src="#drawable/ic_start_wifi_on" />
<ImageButton
android:id="#+id/but_start_gps"
android:src="#drawable/ic_start_gps_on" />
<View
android:id="#+id/view_mid"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_centerVertical="true" />
<Button
android:id="#+id/but_chooseTrack"
android:textColor="#FFFFFF" />
<Button
android:id="#+id/but_go"
android:layout_width="190dp"
android:text="#string/bt_txt_go"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/dateText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/but_start_gps"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/tempText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/dateText"
android:layout_below="#+id/dateText"
android:layout_marginTop="16dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="#+id/but_wifi"
android:src="#drawable/weather" />
</RelativeLayout>
And I have also tried like manual data set but it is not working for me. I dont know what was my mistake could any one tell me how to solve this.
In order to call the method doInBackground of your ASyncTask, you need to add task.execute(params).
params can be either a String or multiple Strings separated by ,
In your case, you should pass to execute the data needed by the method getWeatherData().
i have a table with header row designed in xml. Now i want to add data in the table ( which i get from json array).
I need to scroll data rows vertically without affecting header row, so i created another tablelayout just below the table of header row. Then for each field of each new row, i set layout params in java file and then add the row to second table layout.
But no rows are visible while data is getting printed in the log.I can't figure out where is my mistake.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/rv_table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/rv_head_row"
>
<TextView
android:id="#+id/rv_head_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Desc"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_unit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Unit"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Type"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_range"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Range"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_rv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/RV"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/rv_head_remarks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#999"
android:gravity="center_horizontal|center_vertical"
android:maxLines="1"
android:padding="15dp"
android:text="#string/Remarks"
android:textSize="18sp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/rv_data_table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
</ScrollView>
</LinearLayout>
JAVA CODE:
protected void onCreate(Bundle savedInstanceState) {
Log.i("result value","in result value");
super.onCreate(savedInstanceState);
setContentView(R.layout.results_value);
/*
* connecting with xml
*/
rv_table = (TableLayout) findViewById(R.id.rv_table_layout);
rv_data_table = (TableLayout) findViewById(R.id.rv_data_table_layout);
rv_head_row = (TableRow) findViewById(R.id.rv_head_row);
desc = (TextView)findViewById(R.id.rv_head_desc);
unit= (TextView)findViewById(R.id.rv_head_unit);
type= (TextView)findViewById(R.id.rv_head_type);
range = (TextView)findViewById(R.id.rv_head_range);
rv= (TextView)findViewById(R.id.rv_head_rv);
remarks= (TextView)findViewById(R.id.rv_head_remarks);;
/*
* getting data from server
*/
final Bundle extras = getIntent().getExtras();
try {
js = new JSONObject(){
{
put("CaseId", extras.getString("CaseId"));
}
};
} catch (JSONException e1) {
e1.printStackTrace();
}
Log.v("~~~~~===data is", js.toString());
service = new Services(ResultValueActivity.this);
String rslt=service.getResultValue(js);
try {
JSONObject rs=new JSONObject(rslt);
String p = rs.get("d").toString(); // this is done to
// remove
// starting d: i.e seprating
// first key value pairs
JSONObject temp = new JSONObject(p);
values = temp.getJSONArray("Table"); // values is a array of JSON
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* data received, now starting filling values from json array to table
*/
JSONObject test;
TableRow row;
TextView desc1,unit1,type1,range1;
EditText rv1;
Button remarks1;
for(int i=0;i<values.length();i++)
{
try{
test = values.getJSONObject(i);
row = new TableRow(ResultValueActivity.this);
desc1 = new TextView(ResultValueActivity.this);
unit1 = new TextView(ResultValueActivity.this);
type1 = new TextView(ResultValueActivity.this);
range1 = new TextView(ResultValueActivity.this);
rv1 = new EditText(ResultValueActivity.this);
remarks1 = new Button(ResultValueActivity.this);
/*
* setting text in fields
*/
desc1.setText(test.getString("Description"));
unit1.setText(test.getString("Unit"));
type1.setText(test.getString("ResultType"));
range1.setText(test.getString("RangeValue"));
rv1.setText(test.getString("ResultValue"));
remarks1.setText("Remarks");
desc1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
unit1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
type1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
range1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
rv1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
remarks1.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
row.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f));
Log.i(i+" result value test====>", test.toString());
row.addView(desc1);
row.addView(unit1);
row.addView(type1);
row.addView(range1);
row.addView(rv1);
row.addView(remarks1);
rv_data_table.addView(row,new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
rv_data_table.setVisibility(View.VISIBLE);
Log.i(i+" desc value test====>", desc1.getText().toString());
Log.i(i+" unit value test====>", unit1.getText().toString());
}
catch(JSONException jse)
{
jse.printStackTrace();
}}
Log.i("result Value"," it is done :D :D");
}
Log is printing all the values accurately
1st Table Layout has height of match_parent and thus was not giving space to second table layout, so i changed it to wrap content
I am doing lazyloading to display images and text. I have got everything up and now, trying to add a textview in the view. However, my adapter is overlapping my view. Simpler terms is that, the listview is overlapping the textview. I'm not sure where I'm doing it wrong because the original example is able to display a button just below the listview but I'm not. I'm not getting any errors. My listview just covers the whole screen. Any idea what might be the problem?
main.java
public class main extends Activity {
private static final String targetURL ="http://www.google.com/images";
ListView list;
ProgressDialog dialog;
private String[] mStrings = {};
private String[] dStrings = {};
//TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new TheTask().execute();
list=(ListView)findViewById(R.id.list);
TextView tv = (TextView)findViewById(R.id.tv);
//tv.setText("Text View is displayed");
}
protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{
protected void onPreExecute() {
dialog = ProgressDialog.show(Cats.this, "Retrieving Information", "Please wait for few seconds...", true, false);
dialog.setCancelable(true);
}
protected MyResultClass doInBackground(Void... params) {
searchContent();
MyResultClass result = new MyResultClass();
result.mStrings = mStrings;
result.dStrings = dStrings;
return result;
}
protected void onPostExecute(MyResultClass result) {
dStrings = result.dStrings;
mStrings = result.mStrings;
LazyAdapter adapter = new LazyAdapter(Cats.this, mStrings, dStrings);
list.setAdapter(adapter);
dialog.dismiss();
}
}
class MyResultClass
{
public String[] mStrings;
public String[] dStrings;
}
public void searchContent()
{
String imageC = "";
String textC = "";
try {
URL url = new URL(targetURL);
// Make the connection
URLConnection conn = url.openConnection();
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line = reader.readLine();
Pattern sChar = Pattern.compile("&.*?;");
line.replaceAll("\\<.*?\\>", "");
Matcher msChar = sChar.matcher(line);
while (msChar.find()) line = msChar.replaceAll("");
while (line != null) {
if(line.contains("../../"))
{
int startIndex = line.indexOf("../../") + 6;
int endIndex = line.indexOf(">", startIndex + 1);
String abc = "http://www.google.com/";
String imageSrc = line.substring(startIndex,endIndex);
//complete full url
String xyz = abc +imageSrc;
xyz = xyz.substring(0,xyz.indexOf('"'));
xyz = xyz +";";
xyz = xyz.replaceAll(" ", "%20");
imageC += xyz;
mStrings = imageC.split(";");
line = reader.readLine();
}
if(line.contains("../../") == false)
{
line = reader.readLine();
}
if (line.contains("Gnametag"))
{
int startIndex = line.indexOf("Gnametag") + 10;
int endIndex = line.indexOf("<", startIndex + 1);
String gname = line.substring(startIndex,endIndex);
textC = textC.replaceAll("</span>", "");
textC += "Name: "+gname+ "\n";
}
if (line.contains("Age"))
{
textC += "Age: "+reader.readLine() + "\n" + ";";
textC = textC.replaceAll(" ", "");
dStrings = textC.split(";");
}
if (line.contains("Last Update"))
{
reader.close();
}
}
// Close the reader
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Adapter.java
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.text);;
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(text[position]);
holder.image.setTag(data[position]);
imageLoader.DisplayImage(data[position], activity, holder.image);
return vi;
}
item.xml (used by Adapter.java)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<ImageView
android:id="#+id/image"
android:layout_width="90dip"
android:layout_height="90dip"
android:src="#drawable/stub"
android:scaleType="centerCrop"
android:layout_gravity="left|center_vertical"
/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15dip"/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing 123"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear1"
android:background="#FFFFFF">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"/>
</LinearLayout>
</RelativeLayout>
Just remove the + symbol from this line
android:layout_below="#+id/linear1"
in second linear layout stanza.
When you say the ListView covers the whole screen, do you refer to the result on an actual phone or e.g. the preview in Eclipse? If it shows up correctly in the latter, then likely the issue is somewhere in your Java code.
Anyways, I don't seem to be able to reproduce your problem on either one. However, there are two things I would try:
First, change the order of calls in the onCreate() method of your Activity. Specifically, don't execute your AsyncTask until after you've initialized the ListView, since you're using it in the onPostExecute(). Although this probably won't make a difference, you'd better avoid any chances on a race condition.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list=(ListView)findViewById(R.id.list);
TextView tv = (TextView)findViewById(R.id.tv);
// start task after 'list' is initialized.
new TheTask().execute();
}
Secondly, the layout file containing your ListView is unnecessarily complicated. Preferably you want to keep layouts as simple as possible and limit the nesting for a more flat hierarchy. You could simply get rid of the absolete LinearLayouts, and, if you prefer working with them, change the parent to one. Also, android:orientation="vertical" is not a valid attribute for a RelativeLayout, so make sure you remove that.
Same RelativeLayout, but without the LinearLayouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing 123" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv"
android:layout_marginBottom="30dp" />
</RelativeLayout>
Same behaviour, but with a LinearLayout as root element:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing 123" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginBottom="30dp" />
</LinearLayout>
Add android:layout_alignParentTop="true" to your top LinearLayout (linear1)
<LinearLayout
android:id="#+id/main_xml_linearlayout_one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<TextView
android:id="#+id/main_xml_textview_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/main_xml_linearlayout_two"
android:layout_below="#id/main_xml_linearlayout_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<ListView
android:id="#+id/main_xml_listview_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"/>
</LinearLayout>